我想触发点击事件,即使没有点击按钮。所以我遵循了后angular2 manually firing click event on particular element中提到的结构
但是,我收到一个错误:
Property 'nativeElement' does not exist on type '() => void'
我在网上找不到相关的参考资料。
我想在点击accurateExtract()时触发showAccurateGraphs()按钮。
我的代码是 -
app.component.html
<button type="button" (click)="accurateExtract()" class="btn btn-
dark">Accurate Extract</button>
<button type="button" #accurateExt (click)="showAccurateGraphs()"
class="btn btn-dark">Show Accurate API Graph</button>
app.component.ts
import { Component, OnInit } from '@angular/core';
import { ViewChild, ElementRef, Renderer } from '@angular/core'
constructor(private renderer:Renderer ) {}
@ViewChild('accurateExt') accurateExt: ElementRef;
accurateExtract(){
let event = new MouseEvent('click', {bubbles: true});
this.renderer.invokeElementMethod(
this.accurateExtract.nativeElement,'dispatchEvent', [event]);
}
改变你的代码如下,因为invokeElementMethod()
在角度renderer API 2中不再可用。你的财产中存在拼写错误,你应该将this.accurateExtract
改为this.accurateExt
accurateExtract(){
let event = new MouseEvent('click', {bubbles: true});
this.accurateExt.nativeElement.dispatchEvent(event);
}