简体   繁体   English

带有动态清晰度选项卡的 Angular 表单在添加选项卡后获取 ExpressionChangedAfterItHasBeenCheckedError

[英]Angular form with dynamic clarity tabs get ExpressionChangedAfterItHasBeenCheckedError after add tab

I have reactive angular form with dynamic tabs.我有带有动态标签的反应式 angular 表单。 First - tabs with fields, then tab with button for add tab.首先 - 带有字段的选项卡,然后是带有添加选项卡按钮的选项卡。 If I add tab in form, I get error ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.如果我在表单中添加选项卡,我会收到错误 ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it is checked。 Previous value: 'active: true'.以前的值:“活动:真”。 Current value: 'active: false'.当前值:“活动:假”。

It's clarity changed tab activated status, so I get this error.很明显改变了标签激活状态,所以我收到了这个错误。 Tried use setTimeout - in delete tab it's working - have no error.尝试使用 setTimeout - 在删除选项卡中它正在工作 - 没有错误。 But in adding - it's showed sometimes - offten when active tab not last.但是在添加 - 它有时会显示 - 当活动选项卡没有持续时会出现。 And sometimes I get activated addTab - but I change it to false manually.有时我会激活 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>

Could you please provide any additional information about your project?您能否提供有关您的项目的任何其他信息? There are three main reasons when this error appears:出现此错误的主要原因有以下三个:

  1. You are executing code in AfterViewInit which often happens when working with ViewChild, as it is undefined until AfterViewInit is called.您正在 AfterViewInit 中执行代码,这在使用 ViewChild 时经常发生,因为在调用 AfterViewInit 之前它是未定义的。

  2. You are manipulating the DOM directly (eg using jQuery).您正在直接操作 DOM(例如使用 jQuery)。 Angular cannot always detect these changes and react properly. Angular 无法始终检测到这些变化并做出正确反应。

  3. It can also happen due to race conditions when you are calling functions inside your HTML template.当您在 HTML 模板中调用函数时,它也可能由于竞争条件而发生。

Please follow these three rules and detect corresponding points in you app.请遵循这三个规则并在您的应用程序中检测相应的点。

Your error says it all that there is the change happen which angular's change detector did not record.您的错误说明了发生的变化,而角度的变化检测器没有记录。

Please read the following blog it explains the ChangeDetectionStrategy请阅读以下博客,它解释了ChangeDetectionStrategy

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

In your code you could explicitly call the detectChanges method after your active property is changed.在您的代码中,您可以在更改active属性后显式调用detectChanges方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应形式动态表单验证中的 angular 6“ExpressionChangedAfterItHasBeenCheckedError”添加和删除 - angular 6 "ExpressionChangedAfterItHasBeenCheckedError" in reactive form dynamic form validation add and remove 仅为选项卡内容添加滚动,但不为 Clarity Tabs 中的选项卡标签添加滚动 - Add scroll only for a tab content but not for a tab label in Clarity Tabs 角动态组件和ExpressionChangedAfterItHasBeenCheckedError - Angular Dynamic Components and ExpressionChangedAfterItHasBeenCheckedError 表单验证中的 Angular 5 ExpressionChangedAfterItHasBeenCheckedError - Angular 5 ExpressionChangedAfterItHasBeenCheckedError on form validation Angular中的动态标签:在动态标签内容中导航 - Dynamic tabs in Angular : navigation in dynamic tab content Angular 8 在页面加载后添加 class 错误:ExpressionChangedAfterItHasBeenCheckedError - Angular 8 add class after page load Error: ExpressionChangedAfterItHasBeenCheckedError Angular动态组件加载 - ExpressionChangedAfterItHasBeenCheckedError - Angular dynamic component loading - ExpressionChangedAfterItHasBeenCheckedError 动态组件中的 Angular ExpressionChangedAfterItHasBeenCheckedError - Angular ExpressionChangedAfterItHasBeenCheckedError inside Dynamic Components Angular 4动态页面加载器:ExpressionChangedAfterItHasBeenCheckedError - Angular 4 dynamic page loader : ExpressionChangedAfterItHasBeenCheckedError 动态嵌套反应形式:ExpressionChangedAfterItHasBeenCheckedError - Dynamic nested reactive form: ExpressionChangedAfterItHasBeenCheckedError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM