【发布时间】:2020-03-30 06:34:19
【问题描述】:
获取一个无限循环,尝试遍历此函数调用产生的承诺:
public CUSTOMERS = [
{"id":1,"name":"Crypto Joe", "description":"Love Crypto in the Morning"},
{"id":1,"name":"Crypto Sue", "description":"Love Crypto in the Evening"}
];
loadCustomers():Promise<any[]> {
return of(this.CUSTOMERS).toPromise()
}
这是模板:
<li *ngFor="let customer of loadCustomers() | async">
<h3>{{customer.name}}</h3>
<code> {{customer.description}} </code>
</li>
</ul>
想法?这是一个 stackblitz 演示:
https://stackblitz.com/edit/minimal-angular-ngfor-loop
我更新了演示以使用changeDetection: ChangeDetectionStrategy.OnPush,但它仍然会导致无限循环。也许这只能通过 observables 来完成?
【问题讨论】:
-
另外考虑到您通过
async管道使用Promise,您不妨只提供一个可观察的(return of(this.CUSTOMERS);)。async管道可以很好地处理这个问题。 -
我将原始演示更改为使用`changeDetection:ChangeDetectionStrategy.OnPush`,但它不起作用。知道为什么吗?
标签: javascript angular typescript promise