带有动态清晰度标签的角形表单在添加标签后得到ExpressionChangedAfterItHaHasBeenCheckedError

我有带有动态标签的反应角形式。首先-带有字段的标签,然后是带有添加标签按钮的标签。如果我在表单中添加标签,则会收到错误ExpressionChangedAfterItHaHasBeenCheckedError:检查表达式后,表达式已更改。先前的值:“ active:true”。当前值:“有效:false”。

为了清楚起见,更改了选项卡的激活状态,因此出现此错误。尝试使用setTimeout-在删除选项卡中正常工作-没有错误。但是在添加中-有时会显示-当活动选项卡不持续时会变淡。有时我会激活addTab-但我将其手动更改为false。

onAdd(event) {
  event.stopPropagation();
  const info = this.listGroupDef();
  this.infoList.push(this.fb.group(info));
  this.infoList.markAsDirty();
  this.tabs.push(false);
  setTimeout(() => {
    this.addTab = false;
    this.tabs[this.tabs.length - 1] = true;
  },0);
}
onDelete(event,index: number) {
  setTimeout(() => {
    if (this.tabs[index]) {
      this.tabs[0] = true;
    }
    this.tabs.slice(index,1);
    this.infoList.removeAt(index);
    },0);
  event.stopPropagation();
  }

<clr-tabs>
  <clr-tab *ngFor="let InfoFormGroup of InfoList.controls; let infoIndex = index">
    <button clrTabLink>title<clr-icon shape="times" (click)="onDelete($event,infoIndex)"></clr-icon></button>
    <ng-template [(clrIfactive)]="tabs[infoIndex]">
      <clr-tab-content>
        angular reactive form
      </clr-tab-content>
    </ng-template>
  </clr-tab>
  <clr-tab>
    <button clrTabLink (click)="onAdd($event)"><clr-icon shape="plus-circle"></clr-icon></button>
    <ng-template [(clrIfactive)]="addTab">
      <clr-tab-content>
      </clr-tab-content>
    </ng-template>
  </clr-tab>
</clr-tabs>
mayu_2010 回答:带有动态清晰度标签的角形表单在添加标签后得到ExpressionChangedAfterItHaHasBeenCheckedError

能否请您提供有关您的项目的其他信息?出现此错误的主要原因有三个:

  1. 您正在AfterViewInit中执行代码,这通常在使用ViewChild时发生,因为在调用AfterViewInit之前它是不确定的。

  2. 您正在直接操作DOM(例如,使用jQuery)。 Angular不能总是检测到这些变化并做出正确的反应。

  3. 当您在HTML模板中调用函数时,由于竞争条件也会发生这种情况。

请遵循这三个规则,并在您的应用中检测相应的点。

,

您的错误说明了角度变化检测器未记录的所有变化。

请阅读以下博客,其中介绍了ChangeDetectionStrategy

https://blog.angularindepth.com/everything-you-need-to-know-about-change-detection-in-angular-8006c51d206f

在您的代码中,可以更改detectChanges属性后显式调用active方法。

本文链接:https://www.f2er.com/3118286.html

大家都在问