简体   繁体   English

Angular 12 ngFor 指令中的 ViewChildren 导致 ExpressionChangedAfterItHasBeenCheckedError

[英]Angular 12 ViewChildren in ngFor directive causes ExpressionChangedAfterItHasBeenCheckedError

I've a parent component from which is possible to open various tab containing the child component This is a part of parent component:我有一个父组件,可以从中打开包含子组件的各种选项卡这是父组件的一部分:

....
<p-tabPanel header="{{tab.title}}" *ngFor="let tab of tabs; let i = index" [closable]="true">
    <app-child-tab [modules]="modules" [profile]="tab.body" (onProfileModified)="onProfileModified()">
    </app-cmup-tab>
</p-tabPanel>
....

in.ts: in.ts:

export class ParentComponent implements onInit {
    @ViewChildren(ChildTabComponent) childTabsComponent: QueryList<ChildTabComponent>;
    tabs: any[] = [];
    ...

    ngOnInit() {
        tabs.push({title: 'First', body: firstObject});
        tabs.push({title: 'Second', body: secondObject});
        ...
    }

    onProfileModified() {
       ... do stuff ...
    }
}

The child is:孩子是:

export class ChildTabComponent {
    @Input() profile: any;
}

Opening tabs from parent view, it appears the error:从父视图打开选项卡,出现错误:

ERROR Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked

Is there an error using ViewChildren?使用 ViewChildren 是否有错误?

EDIT: My problem is that in child component I set some modules boolean input values to show in checkbox but opening successive tab, the checkbox aren't correctly set.编辑:我的问题是,在子组件中,我设置了一些模块 boolean 输入值以显示在复选框中,但打开连续选项卡时,复选框设置不正确。 The first tab is properly set but the successive take the value of the first tab.第一个选项卡已正确设置,但后续采用第一个选项卡的值。

I would say because the "tabs" collection is changing as you are pushing values this could be the issue.我会说因为“选项卡”集合在您推送值时正在发生变化,这可能是问题所在。

<div *ngIf="tabs">
  <p-tabPanel header="{{tab.title}}" *ngFor="let tab of tabs; let i = index" [closable]="true">
      <app-child-tab [modules]="modules" [profile]="tab.body" (onProfileModified)="onProfileModified()">
      </app-cmup-tab>
  </p-tabPanel>
</div>

Then in your typescript file: -然后在您的 typescript 文件中:-

    public tabs: any[] = null;
    ...

    ngOnInit() {
       let localTabs: any[] = [];
       localTabs.push({title: 'First', body: firstObject});
       localTabs.push({title: 'Second', body: secondObject});
        ...
       // We now give the component all the info at once
       this.tabs = localTabs; 
    }

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

相关问题 带有ViewChildren的ExpressionChangedAfterItHasBeenCheckedError - ExpressionChangedAfterItHasBeenCheckedError with ViewChildren Angular指令中的ExpressionChangedAfterItHasBeenCheckedError - ExpressionChangedAfterItHasBeenCheckedError in Directive with Angular Angular ngFor类绑定ExpressionChangedAfterItHaHasBeenCheckedError - Angular ngFor class binding ExpressionChangedAfterItHasBeenCheckedError Angular 查看来自嵌套 ngFor 的子级,但按父级 ngFor 分组 - Angular ViewChildren from nested ngFor but group them by parent ngFor 通过Angular中的@ViewChildren通过指令选择组件实例? - Select component instance by directive via @ViewChildren in Angular? 带指令的viewChildren - viewChildren with directive ngFor依赖于另一个ngFor完成渲染-获取ExpressionChangedAfterItHaHasBeenCheckedError [Angular 2] - ngFor depends on another ngFor to finish rendering - getting ExpressionChangedAfterItHasBeenCheckedError [Angular 2] Angular 6 *ngFor 指令不起作用 - Angular 6 *ngFor directive not working Angular ViewChildren 不会立即看到 ngFor 中的所有孩子 - Angular ViewChildren does not see all children from ngFor immediately ExpressionChangedAfterItHasBeenCheckedError角度* ngIf指令引起错误 - ExpressionChangedAfterItHasBeenCheckedError Angular *ngIf directive couses error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM