Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use *ngFor current object outside of the ngFor?

The title might seem weird but what im talking about is basically what this link is doing.

Im look for a way to use the current iterated person in a <tr> outside of the *ngFor scope.

In the link he used ng-repeat-start and ng-repeat-end to include multiple tag withing the ng-repeat. How can i achieve the same behavior withing Angular2 using *ngFor?

like image 973
Kesem David Avatar asked Aug 31 '16 12:08

Kesem David


People also ask

How can I use two ngFor in one div?

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 the purpose of * In ngFor in angular?

The *ngFor directive is used to repeat a portion of HTML template once per each item from an iterable list (Collection). The ngFor is an Angular structural directive and is similar to ngRepeat in AngularJS. Some local variables like Index, First, Last, odd and even are exported by *ngFor directive.

Can we use ngFor in UL tag?

The <ul> tag is not filled by those <li> tag created by *ngFor which makes the "line" of the timeline disappear.

Where can I use ngFor?

You use the *ngFor directive to traverse over an array object and display the data in the UI.


1 Answers

I had to use the <template> tag, which made the iterated objected accessible within it.

<ng-template let-person ngFor [ngForOf]="people">
    <tr>
        <td>{{person.name}}</td>
        <td>{{person.gender}}</td>
    </tr>
    <div>
        <span>{{person.details}}</span>
    </div>
</ng-template>

Hope it'll help somebody

like image 112
Kesem David Avatar answered Sep 30 '22 09:09

Kesem David