特别是,ANGLE是在ngFor的每一次迭代中将元素应用于DOM,还是等到ngFor次迭代完成后再将其全部(父)应用于DOM?

推荐答案

这是ngFor的源代码,这个方法是用Angular 差异标识的变化来调用的,它通知对象或数组中的变化.但如果你注意到代码,there is a 101 which run for each element and inside it we notice for new records, there is a if condition where we run 102, so each row is created one by one!

private _applyChanges(changes: IterableChanges<T>) {
    const viewContainer = this._viewContainer;
    changes.forEachOperation(
        (item: IterableChangeRecord<T>, adjustedPreviousIndex: number|null,
         currentIndex: number|null) => {
          if (item.previousIndex == null) {
            // NgForOf is never "null" or "undefined" here because the differ detected
            // that a new item needs to be inserted from the iterable. This implies that
            // there is an iterable value for "_ngForOf".
            viewContainer.createEmbeddedView(
                this._template, new NgForOfContext<T, U>(item.item, this._ngForOf!, -1, -1),
                currentIndex === null ? undefined : currentIndex);
          } else if (currentIndex == null) {
            viewContainer.remove(
                adjustedPreviousIndex === null ? undefined : adjustedPreviousIndex);
          } else if (adjustedPreviousIndex !== null) {
            const view = viewContainer.get(adjustedPreviousIndex)!;
            viewContainer.move(view, currentIndex);
            applyViewChange(view as EmbeddedViewRef<NgForOfContext<T, U>>, item);
          }
        });

    for (let i = 0, ilen = viewContainer.length; i < ilen; i++) {
      const viewRef = <EmbeddedViewRef<NgForOfContext<T, U>>>viewContainer.get(i);
      const context = viewRef.context;
      context.index = i;
      context.count = ilen;
      context.ngForOf = this._ngForOf!;
    }

    changes.forEachIdentityChange((record: any) => {
      const viewRef = <EmbeddedViewRef<NgForOfContext<T, U>>>viewContainer.get(record.currentIndex);
      applyViewChange(viewRef, record);
    });
  }

Angular相关问答推荐

Angular 动画—淡出适用于DIV,不完全适用于子组件

在不使用散列(#)路由的情况下刷新Angular 8的SPA时,删除404错误

如何动态呈现组件并以Angular 访问它们的方法?

茉莉花-模板中调用了如何判断角管

Angular AuthGuard:不推荐使用 CanActivate 和 CanActivateChild.如何更换它们?

在ngOninit方法中直接VS初始化属性

如果Angular 数据为空,如何设置N/A

如何在 form.value 中禁用复选框

如何将我的数组数据转换为逗号分隔的字符串

无法将项目从 Angular 13 更新到 14

如何使用 Angular 12 material 验证密码和确认密码?

无法推送 Angular 项目 - Github

关闭 Dynamsoft Web Twain 弹出窗口

Angular 2 二传手与 ngOnChanges

如何在 Angular 4 中获取 HttpClient 状态码

错误 TS1086:无法在 Angular 9 的环境上下文中声明访问器

Angular 2 Material - 如何居中进度微调器

在 RxJS Observable 中 flatten数组的最佳方法

Angular将 Select 值转换为 int

formGroup 需要一个 FormGroup 实例