Skip to content

Commit 5dcecae

Browse files
maxokorokovpkozlowski-opensource
authored andcommitted
fix(typeahead): cleans up subscription on component destroy
Closes #620
1 parent d266c42 commit 5dcecae

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/typeahead/typeahead.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import {
1212
ElementRef,
1313
TemplateRef,
1414
forwardRef,
15-
AfterViewChecked
15+
AfterViewChecked,
16+
OnDestroy
1617
} from '@angular/core';
1718
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
18-
import {Observable, Subject} from 'rxjs/Rx';
19+
import {Observable, Subject, Subscription} from 'rxjs/Rx';
1920
import 'rxjs/add/operator/let';
2021
import {Positioning} from '../util/positioning';
2122
import {NgbTypeaheadWindow, ResultTplCtx} from './typeahead-window';
@@ -54,10 +55,11 @@ const NGB_TYPEAHEAD_VALUE_ACCESSOR = {
5455
providers: [NGB_TYPEAHEAD_VALUE_ACCESSOR]
5556
})
5657
export class NgbTypeahead implements OnInit,
57-
AfterViewChecked, ControlValueAccessor {
58+
AfterViewChecked, ControlValueAccessor, OnDestroy {
5859
private _onChangeNoEmit: (_: any) => void;
5960
private _popupService: PopupService<NgbTypeaheadWindow>;
6061
private _positioning = new Positioning();
62+
private _subscription: Subscription;
6163
private _valueChanges = new Subject<string>();
6264
private _windowRef: ComponentRef<NgbTypeaheadWindow>;
6365

@@ -113,8 +115,10 @@ export class NgbTypeahead implements OnInit,
113115
}
114116
}
115117

118+
ngOnDestroy() { this._subscription.unsubscribe(); }
119+
116120
ngOnInit() {
117-
this._valueChanges.let (this.ngbTypeahead).subscribe((results) => {
121+
this._subscription = this._valueChanges.let (this.ngbTypeahead).subscribe((results) => {
118122
if (!results || results.length === 0) {
119123
this.closePopup();
120124
} else {

0 commit comments

Comments
 (0)