Within my template, I have included the following code:
<a *ngFor="let item of navItems"
[routerLink]="item.link"
routerLinkActive="active"
class="navigation-item"
[ngClass]="{'enabled': item.enabled}"
>
<span class="color-{{item.color}}">
<mat-icon>{{item.icon}}</mat-icon>
</span>
<div class="label">{{item.label}}</div>
</a>
The array navItems
is defined within my component as follows:
navItems: NavItem[] = [
{
link: 'link1', label: 'Label 1', icon: 'thumb_up', color: 'green', enabled: true
},
{
link: 'link2', label: 'Label 2', icon: 'thumb_down', color: 'red', enabled: true
},
{
link: 'link3', label: 'Label 3', icon: 'insert_drive_file', color: 'blue', enabled: true
},
{
link: 'link4', label: 'Label 4', icon: 'note_add', color: 'blue', enabled: true
},
];
Although this setup works properly, I now require the ability for the contents of navItems
to dynamically change. I attempted converting the navItems
property into a getter like so:
get navItems(): NavItem[] {
return [
{
link: 'link1', label: 'Label 1', icon: 'thumb_up', color: 'green', enabled: true
},
{
link: 'link2', label: 'Label 2', icon: 'thumb_down', color: 'red', enabled: true
},
{
link: 'link3', label: 'Label 3', icon: 'insert_drive_file', color: 'blue', enabled: true
},
{
link: 'link4', label: 'Label 4', icon: 'note_add', color: 'blue', enabled: true
},
];
}
Unfortunately, when attempting this modification, the browser tab enters an infinite loading loop upon loading the component, requiring termination via the task manager - no console output is generated.
I also experimented with using a regular method call instead of a getter to supply the array, but faced the same issue.
The array being returned consists solely of plain objects with directly assigned string and boolean values, ruling out any nested calls that could trigger recursion.
Is there an error in my approach? Can method calls / getters not be used to provide data for the ngFor directive?