@@ -81,15 +81,19 @@ 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
98
if ( this . _centeringSub && ! this . _centeringSub . closed ) {
95
99
this . _centeringSub . unsubscribe ( ) ;
@@ -136,7 +140,7 @@ export class MatTabBody implements OnInit {
136
140
@Output ( ) _onCentering : EventEmitter < number > = new EventEmitter < number > ( ) ;
137
141
138
142
/** Event emitted before the centering of the tab begins. */
139
- @Output ( ) _beforeCentering : EventEmitter < number > = new EventEmitter < number > ( ) ;
143
+ @Output ( ) _beforeCentering : EventEmitter < boolean > = new EventEmitter < boolean > ( ) ;
140
144
141
145
/** Event emitted when the tab completes its animation towards the center. */
142
146
@Output ( ) _onCentered : EventEmitter < void > = new EventEmitter < void > ( true ) ;
@@ -185,8 +189,9 @@ export class MatTabBody implements OnInit {
185
189
}
186
190
187
191
_onTranslateTabStarted ( e : AnimationEvent ) : void {
188
- if ( this . _isCenterPosition ( e . toState ) ) {
189
- this . _beforeCentering . emit ( ) ;
192
+ const isCentering = this . _isCenterPosition ( e . toState ) ;
193
+ this . _beforeCentering . emit ( isCentering ) ;
194
+ if ( isCentering ) {
190
195
this . _onCentering . emit ( this . _elementRef . nativeElement . clientHeight ) ;
191
196
}
192
197
}
0 commit comments