@@ -29,6 +29,7 @@ import {TemplatePortal, CdkPortalOutlet, PortalHostDirective} from '@angular/cdk
29
29
import { Directionality , Direction } from '@angular/cdk/bidi' ;
30
30
import { Subscription } from 'rxjs' ;
31
31
import { matTabsAnimations } from './tabs-animations' ;
32
+ import { startWith } from 'rxjs/operators' ;
32
33
33
34
/**
34
35
* These position states are used internally as animation states for the tab body. Setting the
@@ -59,28 +60,29 @@ export type MatTabBodyOriginState = 'left' | 'right';
59
60
selector : '[matTabBodyHost]'
60
61
} )
61
62
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 ;
66
67
67
68
constructor (
68
- _componentFactoryResolver : ComponentFactoryResolver ,
69
- _viewContainerRef : ViewContainerRef ,
69
+ componentFactoryResolver : ComponentFactoryResolver ,
70
+ viewContainerRef : ViewContainerRef ,
70
71
@Inject ( forwardRef ( ( ) => MatTabBody ) ) private _host : MatTabBody ) {
71
- super ( _componentFactoryResolver , _viewContainerRef ) ;
72
+ super ( componentFactoryResolver , viewContainerRef ) ;
72
73
}
73
74
74
75
/** Set initial visibility or set up subscription for changing visibility. */
75
76
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
+ } ) ;
84
86
85
87
this . _leavingSub = this . _host . _afterLeavingCenter . subscribe ( ( ) => {
86
88
this . detach ( ) ;
@@ -89,13 +91,9 @@ export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestr
89
91
90
92
/** Clean up centering subscription. */
91
93
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 ( ) ;
99
97
}
100
98
}
101
99
0 commit comments