Reputation: 154
I have a problem with Angular 2. I want to made List HTML like this:
<ul>
<li>
<li>..
</ul>
With links but I don't know how to add iterate argument to HTML object in NG2. It is my code:
<ul>
<li *ngFor="let item of geoNames | filterBy: userFilter"
(click)="showNameG= !showNameG" >
<a>{{ item.name }}</a>
</li>
</ul>
<div *ngIf="showNazwyG">
<show-NameG></show-NameG>
</div>
And I want to add a different name to each of the element li to recognize from has been clicked. In AngularJS worked something like this:
(click)="showNameG{{index}}= !showNameG{{index}}"
Upvotes: 3
Views: 867
Reputation: 657158
*ngFor
provides an index
context variable
<li *ngFor="let item of geoNames | filterBy: userFilter; let i=index"
(click)="showNameG[i]= !showNameG[i]" ><a>{{ item.name }}</a></li>
update
<li *ngFor="let item of geoNames | filterBy: userFilter; let i=index"
(click)="self['showNameG'] + i = !self['showNameG']" ><a>{{ item.name }}</a></li>
export class MyComponent {
get self() {
return this;
}
}
Upvotes: 4
Reputation: 11696
You must add following to your *ngFor:
let index = index;
At the end you should have this:
<ul>
<li *ngFor="let item of geoNames | filterBy: userFilter; let index = index;" (click)="showNameG[index] = !showNameG[index]" ><a>{{ item.name }}</a></li>
</ul>
Here is good description of *ngFor: https://angular.io/docs/ts/latest/api/common/index/NgFor-directive.html
Upvotes: 0
Reputation: 133403
Use item
, create a property showNameG
and toggle its value
<li *ngFor="let item of geoNames | filterBy: userFilter" (click)="item.showNameG=!item.showNameG" ></li>
Upvotes: 1