admin管理员组文章数量:1123374
The menu I want to open when the relevant button is clicked is inside a component. (SideBarCategoriesComponent)
<div class="filters" #sideBar>
...
....
.....
......
</div>
And the relevant button is in a different component.
<div class="item-filter desktop-hide">
<a href="#" class="filter-trigger label" (click)="toggleSidebarMenu()" #toggleButton>
<i class="ri-menu-2-line ri-2x"></i>
<span>Filter</span>
</a>
</div>
export class ProductsComponent {
@ViewChild('toggleButton') toggleButton: ElementRef = {} as ElementRef;
constructor(private shopService: ShopService) {}
toggleSidebarMenu() {
this.shopService.toggleFilterMenu();
}
}
I want the menu to close when clicking outside of the relevant menu and the relevant button.
- The way I found to achieve this is to use @ViewChild.
- But I can't access @ViewChild in another component from within a component.
- So there are conflicts which break the workability.
- How can I overcome this?
export class SideBarCategoriesComponent {
@HostBinding('class.active') filterMenu: boolean = false;
@ViewChild('sideBar') sideBar: ElementRef = {} as ElementRef;
constructor(private shopService: ShopService, private renderer: Renderer2) {
this.renderer.listen('window', 'click', (e: Event) => {
// I need the toggleButton @ViewChild here.
if (
e.target !== this.sideBar.nativeElement &&
e.target !== this.toggleButton.nativeElement
) {
this.shopService.filterMenu.set(false);
}
});
}
ngDoCheck() {
this.filterMenu = this.shopService.filterMenu();
}
}
本文标签: angularHow can I access ViewChild in another component from within a componentStack Overflow
版权声明:本文标题:angular - How can I access @ViewChild in another component from within a component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736566639a1944713.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论