@@ -172,7 +172,11 @@ export class CdkForOf<T> implements CollectionViewer, DoCheck, OnDestroy {
172
172
ngDoCheck ( ) {
173
173
if ( this . _differ && this . _needsUpdate ) {
174
174
const changes = this . _differ . diff ( this . _renderedItems ) ;
175
- this . _applyChanges ( changes ) ;
175
+ if ( ! changes ) {
176
+ this . _updateContext ( ) ;
177
+ } else {
178
+ this . _applyChanges ( changes ) ;
179
+ }
176
180
this . _needsUpdate = false ;
177
181
}
178
182
}
@@ -208,19 +212,18 @@ export class CdkForOf<T> implements CollectionViewer, DoCheck, OnDestroy {
208
212
return newDs . connect ( this ) ;
209
213
}
210
214
211
- /** Apply changes to the DOM. */
212
- private _applyChanges ( changes : IterableChanges < T > | null ) {
213
- // If there are no changes, just update the index and count on the view context and be done.
214
- if ( ! changes ) {
215
- for ( let i = 0 , len = this . _viewContainerRef . length ; i < len ; i ++ ) {
216
- let view = this . _viewContainerRef . get ( i ) as EmbeddedViewRef < CdkForOfContext < T > > ;
217
- view . context . index = this . _renderedRange . start + i ;
218
- view . context . count = this . _data . length ;
219
- view . detectChanges ( ) ;
220
- }
221
- return ;
215
+ /** Update the `CdkForOfContext` for all views. */
216
+ private _updateContext ( ) {
217
+ for ( let i = 0 , len = this . _viewContainerRef . length ; i < len ; i ++ ) {
218
+ let view = this . _viewContainerRef . get ( i ) as EmbeddedViewRef < CdkForOfContext < T > > ;
219
+ view . context . index = this . _renderedRange . start + i ;
220
+ view . context . count = this . _data . length ;
221
+ view . detectChanges ( ) ;
222
222
}
223
+ }
223
224
225
+ /** Apply changes to the DOM. */
226
+ private _applyChanges ( changes : IterableChanges < T > ) {
224
227
// Detach all of the views and add them into an array to preserve their original order.
225
228
const previousViews : EmbeddedViewRef < CdkForOfContext < T > > [ ] = [ ] ;
226
229
for ( let i = 0 , len = this . _viewContainerRef . length ; i < len ; i ++ ) {
@@ -255,6 +258,11 @@ export class CdkForOf<T> implements CollectionViewer, DoCheck, OnDestroy {
255
258
}
256
259
257
260
// We now have a full list of everything to be inserted, so go ahead and insert them.
261
+ this . _insertViews ( insertTuples ) ;
262
+ }
263
+
264
+ /** Insert the RecordViewTuples into the container element. */
265
+ private _insertViews ( insertTuples : RecordViewTuple < T > [ ] ) {
258
266
for ( let i = 0 , len = insertTuples . length ; i < len ; i ++ ) {
259
267
let { view, record} = insertTuples [ i ] ;
260
268
if ( view ) {
0 commit comments