@@ -81,15 +81,19 @@ export class MatTabBodyPortal extends _MatTabBodyPortalBaseClass implements OnIn
8181 ngOnInit ( ) : void {
8282 if ( this . _host . _isCenterPosition ( this . _host . _position ) ) {
8383 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- } ) ;
8984 }
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+ } ) ;
9094 }
9195
92- /** Clean up subscription if necessary . */
96+ /** Clean up centering subscription . */
9397 ngOnDestroy ( ) : void {
9498 if ( this . _centeringSub && ! this . _centeringSub . closed ) {
9599 this . _centeringSub . unsubscribe ( ) ;
@@ -136,7 +140,7 @@ export class MatTabBody implements OnInit {
136140 @Output ( ) _onCentering : EventEmitter < number > = new EventEmitter < number > ( ) ;
137141
138142 /** 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 > ( ) ;
140144
141145 /** Event emitted when the tab completes its animation towards the center. */
142146 @Output ( ) _onCentered : EventEmitter < void > = new EventEmitter < void > ( true ) ;
@@ -185,8 +189,9 @@ export class MatTabBody implements OnInit {
185189 }
186190
187191 _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 ) {
190195 this . _onCentering . emit ( this . _elementRef . nativeElement . clientHeight ) ;
191196 }
192197 }
0 commit comments