如何编写角度8中的viewchild元素引用的click事件

问题描述 投票:0回答:1

在打字稿中尝试单击事件以供元素引用,但不起作用。如何编写单击事件以供元素引用。如果有人知道,请帮忙找到解决方案。

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>
typescript angular7 angular8
1个回答
0
投票

有了这个,您不需要@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');
}
© www.soinside.com 2019 - 2024. All rights reserved.