【问题标题】:Promise and async pipe and Angular ngforPromise 和异步管道和 Angular ngfor
【发布时间】:2018-04-18 03:40:43
【问题描述】:

更新:

我的以下解决方案也有效。我有一个local storage 损坏或类似的东西。这就是不工作的原因。

问题:

你能告诉我如何使用promise/async管道和ngfor吗?我已经尝试如下所示。但它不起作用。

html

 <ion-item *ngFor="let c of contacts | async">
        <ion-label fixed>c.name</ion-label>
 </ion-item>

ts

  contacts: Promise<Contact[]>;
  constructor() {
    this.getContacts()
  }

  getContacts() {
    this.contacts = this.storage.get('contacts').then((val: Contact[]) => {
      console.log('log', val);
      return val;
    });
  }

【问题讨论】:

    标签: angular typescript ionic-framework promise ionic3


    【解决方案1】:

    在你的 .ts 文件上试试这个:

    getContacts() {
       this.contacts = this.storage.get('contacts');
    }
    

    这样,在您的 html 中,异步管道将订阅承诺

    【讨论】:

    • 解决方法完美。但不知何故,如果我在服务中有返回承诺,当我应用异步时,应用程序会挂起
    • OPs 解决方案已经是正确的,并且此代码有效地执行了相同的操作。此答案不包含修复程序,我不明白人们为什么要投票。
    猜你喜欢
    • 2020-01-15
    • 2020-10-08
    • 2019-08-31
    • 2019-04-07
    • 2017-03-27
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 2016-02-18
    相关资源
    最近更新 更多