【问题标题】:Angular 2 *ngFor handles update wrongAngular 2 *ngFor 处理更新错误
【发布时间】:2017-11-03 12:46:17
【问题描述】:

我有以下问题: 我正在使用这个 *ngFor 循环:

<app-sector *ngFor="let subsector of sector.subsectors" [sector]="subsector"></app-sector>

如果数组“子扇区”如下所示:

var subsectors = [
    {text: 'test', title: 'test', id: 'abc'},
    {text: 'test123', title: 'test123', id: 'def'},
    {text: 'test321', title: 'test321', id: 'ghi'}
]

它按预期添加了 3 个“应用程序扇区”组件。那些“app-sector”-组件被添加到“app-sector”-组件中。父级通过我的“应用程序组件”中的“@Input”从父级获取子扇区。

这就是 HTML 结构的样子:

<app-root>
    <app-sector *ngIf="playlist" [sector]="playlist">
        <app-sector *ngFor="let subsector of sector.subsectors" [sector]="subsector"></app-sector>
    </app-sector>
</app-root>

现在问题从这里开始: 当我像这样更新子部门时:

//Add new subsectors
this.subsectors.push({text: 'Blah', title: 'Blah', id: '123'});
this.subsectors.push({text: 'Blah123', title: 'Blah123', id: '456'});
this.subsectors.push({text: 'Blah321', title: 'Blah321', id: '789'});

//Remove old subsectors
this.subsectors.splice(2, 1);
this.subsectors.splice(1, 1);
this.subsectors.splice(0, 1);

*ngFor 不会创建新的组件,只会破坏一些。我真的看不到模式。它似乎是随机决定是销毁还是创建新组件。

这是我迄今为止尝试过的: 使用跟踪。我添加了一个 trackBy 过滤器,它返回了“id”属性,但它没有改变任何东西。方法如下:

<app-sector *ngFor="let subsector of sector.subsectors; trackBy:identify;" [sector]="subsector"></app-sector>

identify(index,item){
    return item.id;
}

然后我尝试了一些我在研究这个问题时看到的技巧,比如使用切片而不是拼接。或者在父母中我尝试这样做:

ngOnChanges(changes) {
    if(this.sector['subsectors']) {
        setTimeout(() => {
            console.log('spliced subsectores for reinitialization');
            this.sector['subsectors'] = this.sector['subsectors'].slice();
        }, 1000);
    }
}

我希望你能帮助我!如果您需要任何进一步的信息来帮助我,请在 cmets 中询问我 :)

亲切的问候。

【问题讨论】:

  • 在 *ngFor 中你迭代了sector.subsectors,但在代码中你修改了this.subsectors。也许我忽略了一些东西,但这对我来说看起来很奇怪。
  • 这是因为我更新了“扇区”类中的子扇区 :) this = 扇区
  • 好的。我不太明白您如何将应用程序扇区列表放入另一个应用程序扇区?我以前从未见过这样做过,不知道它是如何工作的?
  • 好吧,“app-sector”是我应用程序中的一个组件...如果该扇区中有一些子扇区,我只需将该组件放入该组件中...该组件获取一个名为“部门”。这可能有一个非空数组“子扇区”。如果是这种情况,则在“app-sector”内创建另一个“app-sector”,并通过@Input 获取子扇区的扇区。如果这包含子部门同样的事情重复......就是这样。

标签: angular object typescript ngfor


【解决方案1】:

这只是我的两分钱,但我觉得这是translcusion引起的问题


这是什么意思?

在您的模板中,您具有以下结构:

<app-root>
    <app-sector *ngIf="playlist" [sector]="playlist">
        <app-sector *ngFor="let subsector of sector.subsectors" [sector]="subsector"></app-sector>
    </app-sector>
</app-root>

&lt;app-sector&gt; 在其正文中包含另一个&lt;app-sector&gt;,这让我假设您在app-sector 的模板中使用了ng-content somwhere。

过去,*ngForng-content 我也有过类似的意外结果。

我在搜索主题时通常会遇到this answer

父级传递的子级只能投影一次(无论如何 很多都在那里。如果元素没有选择 使用 select 传递的孩子的特定和不同部分 属性,然后一切都投影到第一个 默认选择器(无)。

这不能解释为什么您的初始数据显示 3 次,但它只是我的两分钱。希望对您有所帮助。

【讨论】:

  • 感谢您的回答。我没有使用“ng-content”。如您所见,我通过@Input (->[sector]="subsector") 将子扇区传递给子扇区。 “子扇区”包含组件“应用程序扇区”需要按预期显示的所有信息。这没有问题。唯一的问题是 *ngFor 似乎不会创建新实例,而只会在 Array 更新后销毁“app-sector”的一些实例。
【解决方案2】:

我已经解决了这个问题。我必须在我的@Component({}) 中添加changeDetection: ChangeDetectionStrategy.OnPush。它告诉 Angular2 将任何输入视为不可变的。 - 解决了。

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 2017-05-24
    • 2016-06-02
    • 2016-10-10
    • 2018-08-29
    • 2017-12-16
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多