@@ -15,7 +15,7 @@ import {
1515} from '@angular/core' ;
1616import { NgModel , FormControlName } from '@angular/forms' ;
1717import { CommonModule } from '@angular/common' ;
18- import { Subject } from 'rxjs' ;
18+ import { Subject , Subscription } from 'rxjs' ;
1919import { first , takeUntil } from 'rxjs/operators' ;
2020import { CancelableEventArgs , IBaseEventArgs } from '../../core/utils' ;
2121import {
@@ -91,6 +91,9 @@ export class IgxAutocompleteDirective extends IgxDropDownItemNavigationDirective
9191 excludePositionTarget : true
9292 } ;
9393
94+ /** @hidden @internal */
95+ private subscriptions : Subscription [ ] = [ ] ;
96+
9497 protected id : string ;
9598 protected dropDownOpened$ = new Subject < boolean > ( ) ;
9699 protected get model ( ) {
@@ -303,9 +306,18 @@ export class IgxAutocompleteDirective extends IgxDropDownItemNavigationDirective
303306 // if no drop-down width is set, the drop-down will be as wide as the autocomplete input;
304307 this . target . width = this . target . width || ( this . parentElement . clientWidth + 'px' ) ;
305308 this . target . open ( this . settings ) ;
306- this . target . onSelection . pipe ( takeUntil ( this . dropDownOpened$ ) ) . subscribe ( this . select ) ;
307- this . target . onOpened . pipe ( first ( ) ) . subscribe ( this . highlightFirstItem ) ;
308- this . target . children . changes . pipe ( takeUntil ( this . dropDownOpened$ ) ) . subscribe ( this . highlightFirstItem ) ;
309+
310+ // unsubscribe from previous subscriptions, before creating new subscriptions.
311+ this . unsubscribe ( ) ;
312+
313+ this . subscriptions . push ( this . target . onSelection . pipe ( takeUntil ( this . dropDownOpened$ ) ) . subscribe ( this . select ) ) ;
314+ this . subscriptions . push ( this . target . onOpened . pipe ( first ( ) ) . subscribe ( this . highlightFirstItem ) ) ;
315+ this . subscriptions . push ( this . target . children . changes . pipe ( takeUntil ( this . dropDownOpened$ ) ) . subscribe ( this . highlightFirstItem ) ) ;
316+ }
317+
318+ /** @hidden @internal */
319+ private unsubscribe ( ) {
320+ this . subscriptions . forEach ( subscription => subscription . unsubscribe ( ) ) ;
309321 }
310322
311323 private get collapsed ( ) : boolean {
0 commit comments