Is there a way to pass {{}} arguments to a function inside click?

Viewed 160

I have a problem with angular. I got stuck on this:

I get item list from API and iterate over like this:

<div *ngFor="let item of items">

Now I show results in p:

<p> Number: {{item.value.number}} Name: {{item.value.name}}</p>

And the result is shown. But next I need to pass those values to my function.

<button (click)="addToList({{item.value.number}}, {{item.value.name}})">

This is not working. The console says: Parser Error: Got interpolation ({{}}) where expression was expected.

I searched for a long time and not found answer. How to get around this? I don't know how to get these values to my function in another way. Please help. I use newest angular version.

4 Answers

Just use without the curly brackets

<button (click)="addToList(item.value.number, item.value.name)">

just remove curly braces.

<button (click)="addToList(item.value.number,item.value.name)">

In button tag remove double curly braces. your application will work.

<button (click)="addToList(item.value.number, item.value.name)">

Better way to do this is just pass the whole Object instead of passing each field, you could access the necessary fields within the function, this won't let you to face any issues in future if you want to change parameters, however as others mentioned use without curly braces,

<button (click)="addToList(item)">

now within your function

addToList(item:any){
   //access things you need
}
Related