Angular 4将值从html模板传递到组件中的方法 [英] Angular 4 passing a value from html template to a method in a component

查看:84
本文介绍了Angular 4将值从html模板传递到组件中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Angular 4上遇到了这个问题.

So I am having this issue on Angular 4.

我的html模板标记中有此按钮:

I have this button in my html template markup:

<td><a class="btn btn-danger" (click)="delete()"><i class="fa fa-trash"></i></a></td>

我将数据从* ngFor关联到每个td,所以我有可以在该记录上使用的{{data.id}},但是如何正确地将其分配给我的delete方法,我尝试使用:

I have the data assing to each td from a *ngFor, so I have the {{ data.id }} that I can use on this record, but how can I assign it to my delete method correctly, I tried using:

<td><a class="btn btn-danger" (click)="delete({{ data.id }})"><i class="fa fa-trash"></i></a></td>
<td><a class="btn btn-danger" (click)="delete(id)" [id]={{ data.id }}><i class="fa fa-trash"></i></a></td>

但是似乎没有任何效果,因此,任何建议或指导都将不胜感激.

But none seem to work, so any advice or guidance would be greatly appreciated.

推荐答案

只需传递data.id即可删除功能

just pass data.id to delete function

  <td><a class="btn btn-danger" (click)="delete(data.id)"><i class="fa fa-trash"></i></a></td>

然后在您的删除功能中

    delete(id : any){
console.log(id);
// perform your action
}

这篇关于Angular 4将值从html模板传递到组件中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆