Skip to content

Commit 2e3393a

Browse files
crisbetojelbourn
authored andcommitted
fix(tabs): tab content portal not being cleaned up on destroy (#10661)
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.
1 parent 446ef66 commit 2e3393a

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/lib/tabs/tab-body.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {TemplatePortal, CdkPortalOutlet, PortalHostDirective} from '@angular/cdk
2929
import {Directionality, Direction} from '@angular/cdk/bidi';
3030
import {Subscription} from 'rxjs';
3131
import {matTabsAnimations} from './tabs-animations';
32+
import {startWith} from 'rxjs/operators';
3233

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

6768
constructor(
68-
_componentFactoryResolver: ComponentFactoryResolver,
69-
_viewContainerRef: ViewContainerRef,
69+
componentFactoryResolver: ComponentFactoryResolver,
70+
viewContainerRef: ViewContainerRef,
7071
@Inject(forwardRef(() => MatTabBody)) private _host: MatTabBody) {
71-
super(_componentFactoryResolver, _viewContainerRef);
72+
super(componentFactoryResolver, viewContainerRef);
7273
}
7374

7475
/** Set initial visibility or set up subscription for changing visibility. */
7576
ngOnInit(): void {
76-
if (this._host._isCenterPosition(this._host._position)) {
77-
this.attach(this._host._content);
78-
}
79-
this._centeringSub = this._host._beforeCentering.subscribe((isCentering: boolean) => {
80-
if (isCentering && !this.hasAttached()) {
81-
this.attach(this._host._content);
82-
}
83-
});
77+
super.ngOnInit();
78+
79+
this._centeringSub = this._host._beforeCentering
80+
.pipe(startWith(this._host._isCenterPosition(this._host._position)))
81+
.subscribe((isCentering: boolean) => {
82+
if (isCentering && !this.hasAttached()) {
83+
this.attach(this._host._content);
84+
}
85+
});
8486

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

9092
/** Clean up centering subscription. */
9193
ngOnDestroy(): void {
92-
if (this._centeringSub && !this._centeringSub.closed) {
93-
this._centeringSub.unsubscribe();
94-
}
95-
96-
if (this._leavingSub && !this._leavingSub.closed) {
97-
this._leavingSub.unsubscribe();
98-
}
94+
super.ngOnDestroy();
95+
this._centeringSub.unsubscribe();
96+
this._leavingSub.unsubscribe();
9997
}
10098
}
10199

0 commit comments

Comments
 (0)