MCPcopy Create free account
hub / github.com/angular/angular / _applyChanges

Method _applyChanges

packages/common/src/directives/ng_for_of.ts:286–325  ·  view source on GitHub ↗
(changes: IterableChanges<T>)

Source from the content-addressed store, hash-verified

284 }
285
286 private _applyChanges(changes: IterableChanges<T>) {
287 const viewContainer = this._viewContainer;
288 changes.forEachOperation(
289 (
290 item: IterableChangeRecord<T>,
291 adjustedPreviousIndex: number | null,
292 currentIndex: number | null,
293 ) => {
294 if (item.previousIndex == null) {
295 // NgForOf is never "null" or "undefined" here because the differ detected
296 // that a new item needs to be inserted from the iterable. This implies that
297 // there is an iterable value for "_ngForOf".
298 viewContainer.createEmbeddedView(
299 this._template,
300 new NgForOfContext<T, U>(item.item, this._ngForOf!, -1, -1),
301 currentIndex === null ? undefined : currentIndex,
302 );
303 } else if (currentIndex == null) {
304 viewContainer.remove(adjustedPreviousIndex === null ? undefined : adjustedPreviousIndex);
305 } else if (adjustedPreviousIndex !== null) {
306 const view = viewContainer.get(adjustedPreviousIndex)!;
307 viewContainer.move(view, currentIndex);
308 applyViewChange(view as EmbeddedViewRef<NgForOfContext<T, U>>, item);
309 }
310 },
311 );
312
313 for (let i = 0, ilen = viewContainer.length; i < ilen; i++) {
314 const viewRef = <EmbeddedViewRef<NgForOfContext<T, U>>>viewContainer.get(i);
315 const context = viewRef.context;
316 context.index = i;
317 context.count = ilen;
318 context.ngForOf = this._ngForOf!;
319 }
320
321 changes.forEachIdentityChange((record: any) => {
322 const viewRef = <EmbeddedViewRef<NgForOfContext<T, U>>>viewContainer.get(record.currentIndex);
323 applyViewChange(viewRef, record);
324 });
325 }
326
327 /**
328 * Asserts the correct type of the context for the template that `NgForOf` will render.

Callers 1

ngDoCheckMethod · 0.95

Calls 7

applyViewChangeFunction · 0.85
forEachOperationMethod · 0.65
createEmbeddedViewMethod · 0.65
removeMethod · 0.65
getMethod · 0.65
forEachIdentityChangeMethod · 0.65
moveMethod · 0.45

Tested by

no test coverage detected