我正在尋找的僞代碼如下,但它導致我的瀏覽器掛起。如何在Angular2模板中顯示異步承諾函數的結果?
index.ts
findCustomerById(id) {
return new Promise<string>((resolve, reject) => {
resolve("hi");
});
}
getCustomerNameById(id: string) {
return this.findCustomerById(id);
//findCustomerById returns NEW Promise<string>
}
的index.html
<p>Customer: {{ getCustomerNameById('1') | async }} </p>
//this does not show anything
<p>Customer: {{ getCustomerNameById('1') | async | json }} </p>
//this shows null
<p>Customer: {{ getCustomerNameById('1') | json }}</p>
//this shows the following
{
"__zone_symbol__state": true,
"__zone_symbol__value": "hi"
}
這裏是Plunker:https://plnkr.co/edit/pAKtCZo0Uog2GFzlj50c
你的代碼應該可以正常工作。如果它不請添加代碼'puchServe.findCustomerById()' –
@GünterZöchbauer我已經添加了'findCustomerById' –
瀏覽器控制檯是否顯示任何錯誤? –