Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  182] [ 4]  / answers: 1 / hits: 15068  / 6 Years ago, tue, june 5, 2018, 12:00:00

I have drop down made with Mat select angular component, I need to trigger an event when I clicked outside of the drop down (body of the page).



<mat-select #select multiple (change)=onSubmit($event) [(ngModel)]=emp>
<mat-option *ngFor=let value of filter.default [value]=value>
{{value}}
</mat-option>
</mat-select>


Here is my ts file



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) {

}
}


Its not working properly, please help


More From » angular

 Answers
77

If you want to know when the select panel is closed, use the openedChange event:



<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');
}

[#54271] Thursday, May 31, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
katianatasham

Total Points: 293
Total Questions: 110
Total Answers: 103

Location: Saint Helena
Member since Mon, Jun 28, 2021
3 Years ago
;