【问题标题】:How can I wait for the observable in Angularfire 2?我如何等待 Angularfire 2 中的 observable?
【发布时间】:2016-08-03 15:14:52
【问题描述】:

问题是chat.name 最初显示为空白,因为映射尚未完成。有没有什么方法可以在更新数据之前等待映射?

this.chats = this.af.database.list('/chats/' + this.mainService.getUserId(), {
  query: {
    orderByChild: 'timestamp'
  }
}).map((chats) => {
    return chats.map( chat => {
        chat.name = this.af.database.object(`/users/${chat.uid}`);
        return chat;
    })
});

这是用户界面。

<ul>
    <li *ngFor="let chat of chats | async">
       {{ (chat.name | async)?.name }} : {{  chat.lastMessage }}
    </li>
</ul>

【问题讨论】:

    标签: angular firebase observable angularfire2


    【解决方案1】:

    看到这个答案 - Nested Observables in Angular2 using AngularFire2 not rendering in view

    但我怀疑您需要将 elvis 运算符添加到 HTML 页面中

    {{ chat?.name }}

    【讨论】:

    • 其实我早就看过那个了。我可以显示该值。只是一旦我推送了一条消息,它会保持空白几秒钟然后显示chat.name。
    • 我没有要查看的 UI 代码,所以在这里提供任何指导有点困难?
    • 我刚刚编辑了我的帖子。对于那个很抱歉。问题是,起初它会显示最后一条消息,然后几秒钟后它会显示chat.name。谢谢你。如果我尝试将它包裹在一个 div 周围并执行 *ngIf,那么当有新的更新时它会让用户感到烦恼。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2020-12-05
    相关资源
    最近更新 更多