有没有办法将{{}}参数传递给click内部的函数?

时间:2019-01-14 05:04:12

标签: angular

我对角度有问题。我被困住了:

我从API获取商品列表并像这样遍历:

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

现在我在p中显示结果:

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

并显示结果。但是接下来,我需要将这些值传递给函数。

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

这不起作用。控制台显示:Parser Error: Got interpolation ({{}}) where expression was expected

我搜索了很长时间,但没有找到答案。如何解决这个问题?我不知道如何以其他方式将这些值添加到函数中。请帮忙。 我使用最新的角度版本。

4 个答案:

答案 0 :(得分:3)

只需使用不带花括号

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

答案 1 :(得分:3)

只需取下大括号即可。

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

答案 2 :(得分:1)

在按钮标记中删除双大括号。您的应用程序将运行。

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

答案 3 :(得分:0)

更好的方法是传递整个Object而不是传递每个字段,您可以访问函数中的必要字段,如果您要更改参数,将来不会遇到任何问题,但是正如其他人提到的那样,不带花括号使用

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

现在在您的职能范围内

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