Aquen
Aquen

Reputation: 267

ngFor object in click event

I'm working on an application which shows a list of elements (with ngFor) which also have an "add" Button. When clicking on a button, the element should be shown in a separate list:

<ul>
    <li *ngFor="let trooper of impalasum">
        {{trooper.name}} <button id="btn_{{trooper.name}}" (click)="addToList({{trooper}})">Add</button>
    </li>
</ul> 
<hr>
<ul>
    <li>{{addedTrooper.name}} -> XY</li>
</ul>
addToList(troop: Trooper): void{
    this.addedTrooper = troop;
}

I want to have the trooper as a parameter in the click event, but this way it doesn't work. So how can I do that?

Upvotes: 13

Views: 23603

Answers (1)

yurzui
yurzui

Reputation: 214017

Don't use interpolation within output binding

(click)="addToList(trooper)"

Upvotes: 17

Related Questions