@Component({
selector: 'test-comp',
template: `<div *ngIf="showMe">stuff</div> and more…`
})
export class TestComponent {
showMenu: boolean;
constructor(public elementRef: ElementRef) {
}
@HostListener('document:click', ['$event'])
documentClick(event: MouseEvent) {
//hide my component when there is a document click
this.toggleComponent();
}
toggleComponent() {
// I am trying to programmatically fire a document click here to hide all test-comp if the test-comp
// component itself is clicked
// this.elementRef.nativeElement will select all test-comp component but not sure what to do next
this.showMe = !this.showMe;
}
@H_502_0@我不知道如何在我的toggleComponent方法中以编程方式单击文档.有办法吗?非常感谢!解决方法:
@H_502_0@您可以使用HTMLElement.click()触发任何元素上的单击事件: @H_502_0@document.getElementById('myEl').click() # or the hacky id reference `myEl.click()`
@H_502_0@您不能单击文档,因为它不是渲染元素.但你可以点击整个身体:
@H_502_0@
document.body.click()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。