我可以通过编程方式单击带有 elementref 的按钮元素,但如果按钮是材质按钮,则它不起作用
@ViewChild('findButton') findButton: ElementRef;
clicked() {
console.log('clicked');
}
ngOnInit() {
this.findButton.nativeElement.click()
}
Run Code Online (Sandbox Code Playgroud)
并在模板中,
<div class="example-button-row">
<button #findButton (click)="clicked()" mat-raised-button color="primary">Primary</button>
</div>
Run Code Online (Sandbox Code Playgroud)
mat-raised-button 属性造成了这个问题。这是stackblitz 上的代码
您可以使用以下语法检索ElementRef与 关联的:MatButton
@ViewChild('findButton', { read: ElementRef }) findButton: ElementRef;
Run Code Online (Sandbox Code Playgroud)
请参阅此 stackblitz的演示。
| 归档时间: |
|
| 查看次数: |
2995 次 |
| 最近记录: |