我有带有动态标签的反应角形式。首先-带有字段的标签,然后是带有添加标签按钮的标签。如果我在表单中添加标签,则会收到错误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>