<div class="child2">child2</div>
<app-child2></app-child2> // child2.component
<div class="app">app</div>
<div (click)="open()">button</div>
open(){
$('.child2').trigger('click');
$('.app').trigger('click');
}
我想使用:-
触发let element: HTMLElement = document.querySelector('.child2')as HTMLElement;
element.click();
let element2: HTMLElement = document.querySelector('.app') as HTMLElement;
element2.click();
答案 0 :(得分:0)
1-在div上添加标识符
<div class="child2" #child2>child2</div>
2-在您的组件中,使用@ViewChild装饰器获取HTMLElement
import { ViewChild } from '@angular/core';
export class MyChildComponent {
@ViewChild("child2") myChild2;
public externalClick(){
this.myChild2.nativeElement.click();
}
3-在您的AppComponent中
@ViewChild(MyChildComponent) myChildComp : MyChildComponent;
open(){
this.myChildComp.externalClick();
}