如何获得离子2角2
问题描述:
值按钮上单击事件ID和名字我有一个按钮如何获得离子2角2
<button ion-button icon-left name="0x10000011 (click)="AppService.sendNotify($event)" id="0x10000011" value=1>
<ion-icon name="camera"></ion-icon>
CAM Icon
</button>
当我点击它,我想获得id
属性,我的服务sendNotify
功能。
public sendNotify(data) {
var method = 'SCAN_BUTTON';
var name = data.target.name;
var id = data.target.id;
var value = data.target.value - 0;
console.log("#$#$%$%^$%^#$%^#$%",method, name, value);
}
在这里,我没有得到的按钮id
和name
。相反
#$#$%$%^$%^#$%^#$% SCAN_BUTTON undefined NaN.
任何人都可以帮助我找到我的错误吗?
答
您正在考虑将离子成分ion-button作为基本html button
标记。
它实际上是一个角度分量。检查here。
其内部HTML的样子:根据
<span class="button-inner">
<ng-content></ng-content>
</span>
<div class="button-effect"></div>
的按钮,你点击发送标签的ID和姓名与event.The点击区域可能是button
标签或span
。
您应该简单地将该值作为参数发送。
<button ion-button icon-left name="0x10000011" (click)="AppService.sendNotify($event,'0x10000011','0x10000011')" id="0x10000011" value=1>
<ion-icon name="camera"></ion-icon>
CAM Icon
</button>
+0
感谢您的答案。这解决了我的问题 – ndh
答
您可以使用下面的代码
<button id="ar" name="some" (click)="clicked($event)"> CLICK ME </button>
clicked(event){
console.log(event.srcElement.id)
console.log(event.srcElement.name)
}
答
你的代码在总体上是好的获取值,我也建议使用,而不是srcElement目标。
在你的代码只是错过关闭name属性,尝试把它改成这样:
<button ion-button icon-left name="0x10000011" (click)="AppService.sendNotify($event)" id="0x10000011" value=1>
<ion-icon name="camera"></ion-icon>
CAM Icon
</button>
我希望这将解决您的问题。
为什么不把你的名字/ ID作为函数的参数? –
检查我的答案 –