|
3 | 3 | NgModule, ElementRef, HostListener, ChangeDetectorRef, OnDestroy } from '@angular/core'; |
4 | 4 | import { NgModel, FormControlName } from '@angular/forms'; |
5 | 5 | import { CommonModule } from '@angular/common'; |
6 | | -import { Subject } from 'rxjs'; |
| 6 | +import { Subject, Subscription } from 'rxjs'; |
7 | 7 | import { first, takeUntil } from 'rxjs/operators'; |
8 | 8 | import { CancelableEventArgs, IBaseEventArgs } from '../../core/utils'; |
9 | 9 | import { OverlaySettings, AbsoluteScrollStrategy, IScrollStrategy, IPositionStrategy, AutoPositionStrategy } from '../../services/index'; |
@@ -68,6 +68,9 @@ export class IgxAutocompleteDirective extends IgxDropDownItemNavigationDirective |
68 | 68 | excludePositionTarget: true |
69 | 69 | }; |
70 | 70 |
|
| 71 | + /** @hidden @internal */ |
| 72 | + private subscriptions: Subscription[] = []; |
| 73 | + |
71 | 74 | protected id: string; |
72 | 75 | protected dropDownOpened$ = new Subject<boolean>(); |
73 | 76 | protected get model() { |
@@ -280,9 +283,18 @@ export class IgxAutocompleteDirective extends IgxDropDownItemNavigationDirective |
280 | 283 | // If no drop-down width is set, the drop-down will be as wide as the autocomplete input; |
281 | 284 | this.target.width = this.target.width || (this.parentElement.clientWidth + 'px'); |
282 | 285 | this.target.open(this.settings); |
283 | | - this.target.onSelection.pipe(takeUntil(this.dropDownOpened$)).subscribe(this.select); |
284 | | - this.target.onOpened.pipe(first()).subscribe(this.highlightFirstItem); |
285 | | - this.target.children.changes.pipe(takeUntil(this.dropDownOpened$)).subscribe(this.highlightFirstItem); |
| 286 | + |
| 287 | + // unsubscribe from previous subscriptions, before creating new subscriptions. |
| 288 | + this.unsubscribe(); |
| 289 | + |
| 290 | + this.subscriptions.push(this.target.onSelection.pipe(takeUntil(this.dropDownOpened$)).subscribe(this.select)); |
| 291 | + this.subscriptions.push(this.target.onOpened.pipe(first()).subscribe(this.highlightFirstItem)); |
| 292 | + this.subscriptions.push(this.target.children.changes.pipe(takeUntil(this.dropDownOpened$)).subscribe(this.highlightFirstItem)); |
| 293 | + } |
| 294 | + |
| 295 | + /** @hidden @internal */ |
| 296 | + private unsubscribe() { |
| 297 | + this.subscriptions.forEach(subscription => subscription.unsubscribe()); |
286 | 298 | } |
287 | 299 |
|
288 | 300 | private get collapsed(): boolean { |
|
0 commit comments