【问题标题】:ngFor detection from firebaesngFor 从 firebase 检测
【发布时间】:2022-01-17 07:18:36
【问题描述】:

我有一个页面聊天,与用户与上次消息日期聊天的不同人。

来自 firebase 的 getFunction 也可以工作,我可以在控制台中看到带有正确信息的新数组,但是在 html 页面中,我需要更改视图并返回以获得正确的信息,我有测试 changeDetection 但它不起作用,我已经测试了 ngzone.run() 它可以工作但有延迟..;

this.usersList.push({user:resulta.data(),message:resultaa.data()});

html

<ng-container *ngFor="let usr of usersList">

【问题讨论】:

    标签: angular ionic-framework ngfor


    【解决方案1】:

    而不是推送尝试传播它;

    this.usersList = [
    ...this.usersList,
    {
    user:resulta.data(),
    message: resultaa.data()
    }]
    

    【讨论】:

    • 同样的问题,我需要更改视图并返回查看更改...
    • 回来看看变化是什么意思?
    • 例如在我的 Chrome 浏览器中,我切换选项卡并返回 localhost 选项卡
    【解决方案2】:

    在数组-push后面加上这行this.changeDetectorRef.detectChanges,不行吗?

    问题是,Angular 的变化检测机制不处理数组的内容变化(推/弹出)。因此,您必须手动通知 Angular 发生变化。

    关于Angular-ChangeDetector的官方文档你可以阅读here

    另一种方法是使用slice(),您可以在此查看stackoverflow answerthis

    【讨论】:

    • this.changeDetectorRef.detectChanges 工作,但有延迟,空白数组和 2 秒后它带有正确的信息
    猜你喜欢
    • 2021-08-07
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多