我正在做一个小 Angular 元素,我的表单上有 3 个按钮,我希望在单击它们时收到它们的标题。它们通常被一个一个地点击,到目前为止我已经尝试过:
这是我的 html:
<div class="extra-btns">
<button type="button" title="Title 1" (click)="getMyTitleOne($event)" class="btn xbutton-square" style="font-size: 16px;"><i class="fas fa-newspaper fa-fw"></i></button>
<button type="button" title="Title 2" (click)="getMyTitleTwo($event)" class="btn xbutton-square"><i class="fas fa-fw"></i></button>
</div>
我的 typescript 代码:
getMyTitleOne($event) {
console.log($event.target.title);
}
getMyTitleTwo($event) {
console.log($event.target.title);
}
但在我的控制台中,有时我会得到值,有时我不会,这很奇怪,但大多数时候值是未定义的:
谢谢大家 干杯
最佳答案
使用模板变量:
<button #button1 (click)="log(button1.title)" title="Hello, world !">Click me !</button>
关于javascript - 如何获取单击按钮的标题属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52003795/