@@ -81,19 +81,21 @@ export class MatTabBodyPortal extends _MatTabBodyPortalBaseClass implements OnIn
81
81
ngOnInit ( ) : void {
82
82
if ( this . _host . _isCenterPosition ( this . _host . _position ) ) {
83
83
this . attach ( this . _host . _content ) ;
84
- } else {
85
- this . _centeringSub = this . _host . _beforeCentering . subscribe ( ( ) => {
86
- this . attach ( this . _host . _content ) ;
87
- this . _centeringSub . unsubscribe ( ) ;
88
- } ) ;
89
84
}
85
+ this . _centeringSub = this . _host . _beforeCentering . subscribe ( ( isCentering : boolean ) => {
86
+ if ( isCentering ) {
87
+ if ( ! this . hasAttached ( ) ) {
88
+ this . attach ( this . _host . _content ) ;
89
+ }
90
+ } else {
91
+ this . detach ( ) ;
92
+ }
93
+ } ) ;
90
94
}
91
95
92
- /** Clean up subscription if necessary . */
96
+ /** Clean up centering subscription . */
93
97
ngOnDestroy ( ) : void {
94
- if ( this . _centeringSub && ! this . _centeringSub . closed ) {
95
- this . _centeringSub . unsubscribe ( ) ;
96
- }
98
+ this . _centeringSub . unsubscribe ( ) ;
97
99
}
98
100
}
99
101
@@ -136,7 +138,7 @@ export class MatTabBody implements OnInit {
136
138
@Output ( ) _onCentering : EventEmitter < number > = new EventEmitter < number > ( ) ;
137
139
138
140
/** Event emitted before the centering of the tab begins. */
139
- @Output ( ) _beforeCentering : EventEmitter < number > = new EventEmitter < number > ( ) ;
141
+ @Output ( ) _beforeCentering : EventEmitter < boolean > = new EventEmitter < boolean > ( ) ;
140
142
141
143
/** Event emitted when the tab completes its animation towards the center. */
142
144
@Output ( ) _onCentered : EventEmitter < void > = new EventEmitter < void > ( true ) ;
@@ -185,8 +187,9 @@ export class MatTabBody implements OnInit {
185
187
}
186
188
187
189
_onTranslateTabStarted ( e : AnimationEvent ) : void {
188
- if ( this . _isCenterPosition ( e . toState ) ) {
189
- this . _beforeCentering . emit ( ) ;
190
+ const isCentering = this . _isCenterPosition ( e . toState ) ;
191
+ this . _beforeCentering . emit ( isCentering ) ;
192
+ if ( isCentering ) {
190
193
this . _onCentering . emit ( this . _elementRef . nativeElement . clientHeight ) ;
191
194
}
192
195
}
0 commit comments