简体   繁体   English

ngFor内部的Angular 2组件未更新

[英]Angular 2 components inside ngFor are not updated

I'm struggling for a few hours trying to update the view of my list of item. 我正在努力几个小时,以尝试更新我的商品列表的视图。

I have a component called document-list . 我有一个称为document-list的组件。 This component goes through each document passed to it and include a component called document-list-item . 该组件遍历传递给它的每个文档,并包括一个称为document-list-item的组件。

<div *ngFor="let document of documents">
    <document-list-item [document]="document"></document-list-item>
</div>

When I update the list of document, the children views are not updated. 当我更新文档列表时,子视图不会更新。

I tried those in the documents accessor of the document-list component : 我在document-list组件的documents访问器中尝试过这些:

public set documents(value: DocumentDetails[]) {
    this._documents = value;
    // used one by one
    this.changeDetector.detectChanges(); => doesn't work
    this.changeDetector.markForCheck(); => doesn't work
    this.appRef.tick(); => throw an error about recursivity
}

I also tried to manually update the list in the setters (well placed console.log showed me that it goes in) : 我还尝试了手动更新设置器中的列表(放置良好的console.log向我显示了它的加入):

private _documents: DocumentDetails[] = [];
@Input()
public set documents(value: DocumentDetails[]) {
    this._documents = [];
    for (let i = 0; i < value.length; i++) {
        this._documents[i] = value[i];
    }
}
public get documents() {
    return this._documents;
}

The only thing that works was doing this in the accessor : 唯一有效的方法是在访问器中执行此操作:

public set documents(value: DocumentDetails[]) {
    this.documents = [];
    setTimeout(() => {
        this._documents = value;
    }, 0);
}

It worked but it showed a blinking list each time the documents array were updated, which is not acceptable. 它可以工作,但是每次文档数组更新时,都会显示一个闪烁的列表,这是不可接受的。

Any help is welcome 欢迎任何帮助

You have: 你有:

<document-list-item [document]="document"></document-list-item>

So you should have in your child class: 因此,您应该在孩子班级上:

@Input()
document: any;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM