Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 ngfor let two variables at the same time

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.

like image 393
dev Avatar asked Oct 04 '18 00:10

dev


People also ask

Can we use two ngFor in Angular?

You can't use multiple *ngFor s in the same element as those are structural directives and angular handles them in a specific way (basically for each structural directive it creates an ng-template element which then holds the directive itself, you can read more about it here: https://angular.io/guide/structural- ...

What is * in ngFor Angular?

The * character before ngFor creates a parent template. It's a shortcut to the following syntax: template=“ngFor let item of items” .

Is ngFor a loop?

app.component.html First, the *ngFor directive is used to iterate over the values of the options array. It's like a forEach loop where a single value is considered, putting it in the value attribute of an option element.

How do you break ngFor?

There is no option to break ngFor . You can use a custom pipe that doesn't return values after the element where the condition is met.


1 Answers

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>
like image 165
Rafael Avatar answered Sep 27 '22 19:09

Rafael