Skip to content

Commit

Permalink
fix(tabs): tab content portal not being cleaned up on destroy (#10661)
Browse files Browse the repository at this point in the history
Fixes the underlying `MatTabBodyPortal` not cleaning up correctly on destroy, because it overrides the `ngOnDestroy` method from the `CdkPortalOutlet`. Also does some general cleanup around the tab body component.
  • Loading branch information
crisbeto authored and jelbourn committed Apr 5, 2018
1 parent 446ef66 commit 2e3393a
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/lib/tabs/tab-body.ts
Expand Up @@ -29,6 +29,7 @@ import {TemplatePortal, CdkPortalOutlet, PortalHostDirective} from '@angular/cdk
import {Directionality, Direction} from '@angular/cdk/bidi';
import {Subscription} from 'rxjs';
import {matTabsAnimations} from './tabs-animations';
import {startWith} from 'rxjs/operators';

/**
* These position states are used internally as animation states for the tab body. Setting the
Expand Down Expand Up @@ -59,28 +60,29 @@ export type MatTabBodyOriginState = 'left' | 'right';
selector: '[matTabBodyHost]'
})
export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestroy {
/** A subscription to events for when the tab body begins centering. */
private _centeringSub: Subscription;
/** A subscription to events for when the tab body finishes leaving from center position. */
private _leavingSub: Subscription;
/** Subscription to events for when the tab body begins centering. */
private _centeringSub = Subscription.EMPTY;
/** Subscription to events for when the tab body finishes leaving from center position. */
private _leavingSub = Subscription.EMPTY;

constructor(
_componentFactoryResolver: ComponentFactoryResolver,
_viewContainerRef: ViewContainerRef,
componentFactoryResolver: ComponentFactoryResolver,
viewContainerRef: ViewContainerRef,
@Inject(forwardRef(() => MatTabBody)) private _host: MatTabBody) {
super(_componentFactoryResolver, _viewContainerRef);
super(componentFactoryResolver, viewContainerRef);
}

/** Set initial visibility or set up subscription for changing visibility. */
ngOnInit(): void {
if (this._host._isCenterPosition(this._host._position)) {
this.attach(this._host._content);
}
this._centeringSub = this._host._beforeCentering.subscribe((isCentering: boolean) => {
if (isCentering && !this.hasAttached()) {
this.attach(this._host._content);
}
});
super.ngOnInit();

this._centeringSub = this._host._beforeCentering
.pipe(startWith(this._host._isCenterPosition(this._host._position)))
.subscribe((isCentering: boolean) => {
if (isCentering && !this.hasAttached()) {
this.attach(this._host._content);
}
});

this._leavingSub = this._host._afterLeavingCenter.subscribe(() => {
this.detach();
Expand All @@ -89,13 +91,9 @@ export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestr

/** Clean up centering subscription. */
ngOnDestroy(): void {
if (this._centeringSub && !this._centeringSub.closed) {
this._centeringSub.unsubscribe();
}

if (this._leavingSub && !this._leavingSub.closed) {
this._leavingSub.unsubscribe();
}
super.ngOnDestroy();
this._centeringSub.unsubscribe();
this._leavingSub.unsubscribe();
}
}

Expand Down

0 comments on commit 2e3393a

Please sign in to comment.