Answer the question
In order to leave comments, you need to log in
HostListener document:click on one component among many copies?
Good day,
There is a component that displays a string, when clicked, a form pops up for editing this string.
This component is used in tables, that is, in one table ~ 50 copies of this component.
Well, you need to make sure that when you click outside the component area, the component closes.
I write like this:
...
@HostListener('document:keyup', ['$event'])
public handleClick(event) {
if (!this.editable.nativeElement.contains(event.target)) this.onClose();
}
Answer the question
In order to leave comments, you need to log in
You can assign closure handling to a special directive, which might look like
@Directive({
selector: '[outsideClickHandler]',
})
export class OutsideClickDirective {
@Output('outsideClickHandler') close = new EventEmitter();
isOpen: boolean = false;
constructor(private elRef: ElementRef) {}
@HostListener('document:click', ['$event'])
public handleClick(event) {
if (!this.isOpen) {
this.isOpen = true;
} else if (!this.elRef.nativeElement.contains(event.target)) {
this.close.emit();
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question