Skip to content

Commit e28e386

Browse files
author
Çağatay Çivici
committed
1 parent 084b318 commit e28e386

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

components/dropdown/dropdown.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,6 @@ export class Dropdown implements OnInit,AfterViewInit,AfterViewChecked,DoCheck,O
134134
ngOnInit() {
135135
this.optionsToDisplay = this.options;
136136
this.updateSelectedOption(null);
137-
138-
this.documentClickListener = this.renderer.listenGlobal('body', 'click', () => {
139-
if(!this.selfClick&&!this.itemClick) {
140-
this.panelVisible = false;
141-
}
142-
143-
this.selfClick = false;
144-
this.itemClick = false;
145-
});
146137
}
147138

148139
ngDoCheck() {
@@ -290,11 +281,14 @@ export class Dropdown implements OnInit,AfterViewInit,AfterViewChecked,DoCheck,O
290281
this.domHandler.relativePosition(panel, container);
291282

292283
this.domHandler.fadeIn(panel,250);
284+
285+
this.bindDocumentClickListener();
293286
}
294287
}
295288

296289
hide() {
297290
this.panelVisible = false;
291+
this.unbindDocumentClickListener();
298292
}
299293

300294
onInputFocus(event) {
@@ -416,12 +410,29 @@ export class Dropdown implements OnInit,AfterViewInit,AfterViewChecked,DoCheck,O
416410
this.domHandler.findSingle(this.el.nativeElement, 'input[readonly]').focus();
417411
}
418412

419-
ngOnDestroy() {
420-
this.initialized = false;
421-
413+
bindDocumentClickListener() {
414+
if(!this.documentClickListener) {
415+
this.documentClickListener = this.renderer.listenGlobal('body', 'click', () => {
416+
if(!this.selfClick&&!this.itemClick) {
417+
this.panelVisible = false;
418+
}
419+
420+
this.selfClick = false;
421+
this.itemClick = false;
422+
});
423+
}
424+
}
425+
426+
unbindDocumentClickListener() {
422427
if(this.documentClickListener) {
423428
this.documentClickListener();
424429
}
430+
}
431+
432+
ngOnDestroy() {
433+
this.initialized = false;
434+
435+
unbindDocumentClickListener();
425436

426437
if(this.appendTo) {
427438
this.el.nativeElement.appendChild(this.panel);

0 commit comments

Comments
 (0)