我使用Mat选择角度组件进行下拉,当我在下拉菜单(页面正文)外面点击时,我需要触发一个事件。
<mat-select #select multiple (change)="onSubmit($event)" [(ngModel)]="emp">
<mat-option *ngFor="let value of filter.default" [value]="value">
{{value}}
</mat-option>
</mat-select>
这是我的ts文件
export class AnotherComponent {
public text: String;
@HostListener('document:click', ['$event'])
clickout(event) {
if(this.eRef.nativeElement.contains(event.target)) {
console.log("clicked inside");
} else {
console.log("clicked outside");
}
}
constructor(private eRef: ElementRef) {
}
}
它无法正常工作,请帮忙
答案 0 :(得分:2)
如果您想知道选择面板何时关闭,请使用openedChange
事件:
<mat-select #select multiple (change)="onSubmit($event)" [(ngModel)]="emp"
(openedChange)="openedChange($event)">
<mat-option *ngFor="let value of filter.default" [value]="value">
{{value}}
</mat-option>
</mat-select>
openedChange(opened: boolean) {
console.log(opened ? 'opened' : 'closed');
}