Angular4变化检测ExpressionChangedAfterItHasBeenCheckedError

时间:2017-07-05 09:34:55

标签: angular typescript angular2-changedetection

由于$digest - 循环检查所有内容,我试图解决AngularJS中不存在的问题。

我试图创建一个可以初始化自身(异步)的组件,并通知其周围的容器已完成加载。

我有这个伪表:

<my-table>
    <ng-container *ngFor="let row in rows">
        <my-table-row
            [row]="row"
            [isLoading]="row.isLoading"
            (click)="onClickRow(row)"
        ></my-table-row>
        <my-detail-row *ngIf="row.isExpanded">
            <my-component
                [data]="row"
            ></my-component>
        ></my-detail-row>
    </ng-container>
</my-table>

在周围的组件(包含rows)中,我有onClickRow(row)函数切换row.isExpanded

my-component组件中,我想将row.isLoading设置为true(通知&#34;父级&#34;它正在加载的行),然后在API调用后将其设置为false完成。

@Input() data: any;

ngOnInit() {
    this.task.isLoading = true; // PROBLEM

    setTimeout(() => { // simulate API call
        this.task.isLoading = false; // no problem
    }, 2000);
}

现在我收到此错误:Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: 'true'.

我想这是因为更改从my-component上升到ng-container,但后来不是my-table-row

我有一个解决方法,因为我可以在setTimeout()的第一个设置周围使用第二个this.task.isLoading,但是这会产生一些popin效果,我想找到一个更清洁的解决方案可能的。

是否有人有任何建议,这是如何运作的?

1 个答案:

答案 0 :(得分:0)

I found a solution that I can live with.

You can inject the "parent" into your component (similar to require in AngularJS).

For this to work, you need to combine my-table-rowand my-detail-row into something like my-item-row. This has the added benefit, that you can put the toggling-logic into the component and don't have to re-implement it on every use.

my-item-row.component.html:

<div
    class="item-row"
    (click)="onToggleDetail()"
>
    <div class="cell">...</div>

    <div *ngIf="isLoading" class="loading"></div>
</div>

<div
    *ngIf="isExpanded"
    [hidden]="!isInitialized"
    class="detail-row"
>
    ...
</div>

my-item-row.component.ts:

import { Component } from '@angular/core';
import { Input, ChangeDetectorRef } from '@angular/core';

@Component({...})
export class MyItemRowComponent {
    ...

    isInitialized: boolean = false;
    isLoading: boolean = false;
    isExpanded: boolean = false;

    constructor(private changeDetector: ChangeDetectorRef) { }

    onToggleDetail() : void {
        this.isExpanded = !this.isExpanded;
    }

    public startLoading() : void {
        this.isLoading = true;
        this.isInitialized = false;

        this.changeDetector.detectChanges(); // trigger re-evaluate
    }

    public stopLoading() : void {
        this.isLoading = false;
        this.isInitialized = true;

        this.changeDetector.detectChanges(): // trigger re-evaluate
    }
}

Now, you can inject this into my-component, like this:

my-component.component.ts:

import { Component } from '@angular/core';
import { Input, Host, Optional } from '@angular/core';

import { MyItemRowComponent } from '...';

@Component({...})
export class MyComponentComponent {
    ...

    // Optional, because there may be a case where we want to use it outside of a row
    constructor(@Optional() @Host() private parent: MyItemRowComponent) { }

    ngOnInit() { // or ngAfterViewInit, doesn't matter
        if (this.parent) {
            this.parent.startLoading();
        }

        setTimeout(() => { // simulate API call
            if (this.parent) {
                this.parent.stopLoading();
            }
        }, 2000);
    }
}

This is my current solution.

This lead to a different issue with the my-component being initiated, even when it wasn't expanded yet, see here:

Angular4 ng-content gets built when ngIf is false