每当用户单击不在工具栏上的任何地方时,我都试图将其关闭。我尝试了双向数据绑定,现在尝试使用rxjs' Subject
<app-sidenav [openedSubject]="openedSubject"></app-sidenav>
openedSubject = new Subject<boolean>();
@HostListener('click', ['$event']) clickedInside($event) {
this.openedSubject.next(false);
}
@Input() openedSubject: Subject<boolean>;
@ViewChild('sidenav') sidenav: MatSidenav;
ngAfterContentInit() {
this.openedSubject.subscribe(opened => {
if(this.sidenav.opened) opened || this.sidenav.toggle();
});
}
toggle() {
this.openedSubject.next(!this.sidenav.opened);
}
<mat-sidenav-container>
<mat-sidenav #sidenav><mat-toolbar color="primary">Menu</mat-toolbar>
</mat-sidenav>
<mat-sidenav-content>
<mat-toolbar color="primary">
<button (click)="toggle()">toggle</button>
</mat-toolbar>
</mat-sidenav-content>
</mat-sidenav-container>
https://stackblitz.com/edit/ng-sidenav-experiments
但是似乎存在逻辑错误。 click
event涵盖了整个文档,而其中应排除sidenav和工具栏。
答案 0 :(得分:0)
使其正常工作。问题是我对HostListener
的使用,而我真的只是想像这样(click)
:
<sidenav [openedSubject]="openedSubject"></sidenav>
<fake-route-view (click)="dismissSidebar()"></fake-route-view>