mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(autocomplete): error when closing destroyed panel (#5446)
Fixes an error that was being thrown when attempting to close the panel of an autocomplete that is inside a destroyed view. The issue is that `closePanel` can end up getting called multiple times in a row, which leads to an attempt to call `detectChanges` on a destroyed `ChangeDetectorRef`. Fixes #5413.
This commit is contained in:
committed by
mmalerba
parent
372549c378
commit
880e6d513d
@@ -170,6 +170,10 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
|
||||
|
||||
/** Closes the autocomplete suggestion panel. */
|
||||
closePanel(): void {
|
||||
if (!this.panelOpen) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._overlayRef && this._overlayRef.hasAttached()) {
|
||||
this._overlayRef.detach();
|
||||
this._closingActionsSubscription.unsubscribe();
|
||||
|
||||
@@ -221,17 +221,31 @@ describe('MdAutocomplete', () => {
|
||||
});
|
||||
}));
|
||||
|
||||
it('should close the panel programmatically', () => {
|
||||
it('should close the panel programmatically', async(() => {
|
||||
fixture.componentInstance.trigger.openPanel();
|
||||
fixture.detectChanges();
|
||||
|
||||
fixture.componentInstance.trigger.closePanel();
|
||||
fixture.detectChanges();
|
||||
fixture.whenStable().then(() => {
|
||||
fixture.componentInstance.trigger.closePanel();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(fixture.componentInstance.trigger.panelOpen)
|
||||
.toBe(false, `Expected closing programmatically to set the panel state to closed.`);
|
||||
expect(overlayContainerElement.textContent)
|
||||
.toEqual('', `Expected closing programmatically to close the panel.`);
|
||||
fixture.whenStable().then(() => {
|
||||
expect(fixture.componentInstance.trigger.panelOpen)
|
||||
.toBe(false, `Expected closing programmatically to set the panel state to closed.`);
|
||||
expect(overlayContainerElement.textContent)
|
||||
.toEqual('', `Expected closing programmatically to close the panel.`);
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should not throw when attempting to close the panel of a destroyed autocomplete', () => {
|
||||
const trigger = fixture.componentInstance.trigger;
|
||||
|
||||
trigger.openPanel();
|
||||
fixture.detectChanges();
|
||||
fixture.destroy();
|
||||
|
||||
expect(() => trigger.closePanel()).not.toThrow();
|
||||
});
|
||||
|
||||
it('should hide the panel when the options list is empty', async(() => {
|
||||
|
||||
Reference in New Issue
Block a user