I need to assign two variable in for. Something like this in Angular 5+
<div *ngFor="let a of apple, let b of ball">
<a [routerLink]="['/ball',b]">
{{a}}
</a>
</div>
Any suggestions please?
Advance thanks for your help.
I need to assign two variable in for. Something like this in Angular 5+
<div *ngFor="let a of apple, let b of ball">
<a [routerLink]="['/ball',b]">
{{a}}
</a>
</div>
Any suggestions please?
Advance thanks for your help.
It appears that your arrays are the same length, i.e., Parallel Arrays. You should avoid nesting ngFor
.
You can use the index of one array, to look into the other:
<div *ngFor="let a of apple; let i = index">
<a [routerLink]="['/ball', ball[i]]">{{a}}</a>
</div>
It is much more natural, instead of using two arrays, to organise your data as an array of objects like so:
this.things = [
{
apple: 'apple1',
ball: 'ball1'
},
{
apple: 'apple2',
ball: 'ball2'
}
]
You can then iterate over this array like so:
<div *ngFor="let thing of things">
<a [routerLink]="['/ball', thing.ball]">
{{ thing.apple }}
</a>
</div>
This is also far more readable and standard practice.
You can use the index of ngFor to access the elements of the second array.
<ng-container *ngFor="let a of apple; index as i">
<h1>{{a}}</h1>
<h2>{{b[i]}}</h2>
</ng-container>