You want to display a number related to the current index inside *ngFor. You are currently attempting to increment a component-level property in every iteration of the loop.
There are 2 problems with your current approach:
- Interpolation expressions must be read-only
- You are attempting to use a component-level property that is different for each item of an array. Every time you increment the property, all HTML bound to that property will show the updated value.
I would approach this in a model-based way. Build a nested array of template ids that matches the structure of your existing nested array.
You haven't given any code [edit - at the time of writing], so I am going to use a nested array of strings, and build a corresponding nested array of `templateIds
component.ts
templates: string[][];
templateIds: number[][];
ngOnInit() {
this.templates = [
[ 'a', 'b', 'c' ],
[ 'd' ],
[ 'e', 'f' ],
[ 'g', 'h', 'i' ]
];
this.templateIds = [];
let templateId = 0;
this.templates.forEach((x, i) => {
this.templateIds.push(x.map(x => templateId++));
});
}
You can then use the index from *ngFor to access the templateIds.
<div *ngFor="let row of templates; index as i">
<span *ngFor="let col of row; index as j">
{{col}} {{templateIds[i][j]}}
</span>
</div>
If it's appropriate, you could alternatively add the templateId to your original nested array, but I think maintaining an external array keeps your code cleaner.
DEMO: https://stackblitz.com/edit/angular-yc6zvh