@@ -5,7 +5,13 @@ import {
5
5
NgZone ,
6
6
QueryList ,
7
7
ElementRef ,
8
- ViewEncapsulation , ContentChildren , Output , EventEmitter , Optional
8
+ ViewEncapsulation ,
9
+ ContentChildren ,
10
+ Output ,
11
+ EventEmitter ,
12
+ Optional ,
13
+ AfterContentChecked ,
14
+ AfterContentInit ,
9
15
} from '@angular/core' ;
10
16
import { RIGHT_ARROW , LEFT_ARROW , ENTER , Dir , LayoutDirection } from '../core' ;
11
17
import { MdTabLabelWrapper } from './tab-label-wrapper' ;
@@ -44,7 +50,7 @@ const EXAGGERATED_OVERSCROLL = 60;
44
50
'[class.md-tab-header-rtl]' : "_getLayoutDirection() == 'rtl'" ,
45
51
}
46
52
} )
47
- export class MdTabHeader {
53
+ export class MdTabHeader implements AfterContentChecked , AfterContentInit {
48
54
@ContentChildren ( MdTabLabelWrapper ) _labelWrappers : QueryList < MdTabLabelWrapper > ;
49
55
50
56
@ViewChild ( MdInkBar ) _inkBar : MdInkBar ;
@@ -106,12 +112,12 @@ export class MdTabHeader {
106
112
this . _tabLabelCount = this . _labelWrappers . length ;
107
113
}
108
114
109
-
110
115
// If the selected index has changed, scroll to the label and check if the scrolling controls
111
116
// should be disabled.
112
117
if ( this . _selectedIndexChanged ) {
113
118
this . _scrollToLabel ( this . _selectedIndex ) ;
114
119
this . _checkScrollingControls ( ) ;
120
+ this . _alignInkBarToSelectedTab ( ) ;
115
121
this . _selectedIndexChanged = false ;
116
122
}
117
123
@@ -123,18 +129,6 @@ export class MdTabHeader {
123
129
}
124
130
}
125
131
126
- /**
127
- * Waits one frame for the view to update, then updates the ink bar and scroll.
128
- * Note: This must be run outside of the zone or it will create an infinite change detection loop.
129
- */
130
- ngAfterViewChecked ( ) : void {
131
- this . _zone . runOutsideAngular ( ( ) => {
132
- window . requestAnimationFrame ( ( ) => {
133
- this . _alignInkBarToSelectedTab ( ) ;
134
- } ) ;
135
- } ) ;
136
- }
137
-
138
132
_handleKeydown ( event : KeyboardEvent ) {
139
133
switch ( event . keyCode ) {
140
134
case RIGHT_ARROW :
@@ -149,6 +143,21 @@ export class MdTabHeader {
149
143
}
150
144
}
151
145
146
+ /**
147
+ * Aligns the ink bar to the selected tab on load.
148
+ */
149
+ ngAfterContentInit ( ) {
150
+ this . _alignInkBarToSelectedTab ( ) ;
151
+ }
152
+
153
+ /**
154
+ * Callback for when the MutationObserver detects that the content has changed.
155
+ */
156
+ _onContentChanges ( ) {
157
+ this . _updatePagination ( ) ;
158
+ this . _alignInkBarToSelectedTab ( ) ;
159
+ }
160
+
152
161
/**
153
162
* Updating the view whether pagination should be enabled or not
154
163
*/
@@ -178,7 +187,7 @@ export class MdTabHeader {
178
187
_isValidIndex ( index : number ) : boolean {
179
188
if ( ! this . _labelWrappers ) { return true ; }
180
189
181
- const tab = this . _labelWrappers . toArray ( ) [ index ] ;
190
+ const tab = this . _labelWrappers ? this . _labelWrappers . toArray ( ) [ index ] : null ;
182
191
return tab && ! tab . disabled ;
183
192
}
184
193
@@ -282,7 +291,10 @@ export class MdTabHeader {
282
291
* should be called sparingly.
283
292
*/
284
293
_scrollToLabel ( labelIndex : number ) {
285
- const selectedLabel = this . _labelWrappers . toArray ( ) [ labelIndex ] ;
294
+ const selectedLabel = this . _labelWrappers
295
+ ? this . _labelWrappers . toArray ( ) [ labelIndex ]
296
+ : null ;
297
+
286
298
if ( ! selectedLabel ) { return ; }
287
299
288
300
// The view length is the visible width of the tab labels.
@@ -359,6 +371,11 @@ export class MdTabHeader {
359
371
const selectedLabelWrapper = this . _labelWrappers && this . _labelWrappers . length
360
372
? this . _labelWrappers . toArray ( ) [ this . selectedIndex ] . elementRef . nativeElement
361
373
: null ;
362
- this . _inkBar . alignToElement ( selectedLabelWrapper ) ;
374
+
375
+ this . _zone . runOutsideAngular ( ( ) => {
376
+ requestAnimationFrame ( ( ) => {
377
+ this . _inkBar . alignToElement ( selectedLabelWrapper ) ;
378
+ } ) ;
379
+ } ) ;
363
380
}
364
381
}
0 commit comments