在打字稿中尝试单击事件以供元素引用,但不起作用。如何编写单击事件以供元素引用。如果有人知道,请帮忙找到解决方案。
app.component.ts:
@ViewChild('testElem') el:ElementRef;
@ViewChild('testElem2') el2:ElementRef;
this.el.on('click',()=>{
alert("test");
});
app.component.html:
<div #el>testElem</div>
<div #el2>testElem2</div>
有了这个,您不需要@ViewChild
来编写单击事件。
app.component.html
<div #el (click)="onClick()">testElem</div>
app.component.ts
// a method on the class
onClick() {
console.log('element clicked');
alert('test');
}