我有一個可觀察的字符串數組const obs$: Observable<string[]>
作爲屬性在我的組件上。雖然我可以在*ngIf
語句上成功使用async
管道,但通過數組索引器(obs$ | async)[0]
訪問管道時管道會失敗。Angular2:數組索引異步管道
例子:
<!-- evaluates the array emmitted by obs$ for truthyness -->
<div *ngIf="obs$ | async">
<!-- only shown if obs$ emitted an array with length > 0 -->
<!-- but this fails with error: Cannot read property '0' of null -->
<img [src]="(obs$ | async)[0]">
</div>
OBS $在部件的構造方法設置的實例,所以OBS $不應當模板數據綁定不確定的。
如何正確訪問模板中的數組元素?
目前已經已經提交了[錯誤報告](HTTPS ://github.com/angular/angular/issues/10293) - 問題將在RC.5中修復 – rinukkusu
本身不重複;如果您想遍歷數組,則重複答案中的解決方案僅適用於(使用ngFor);沒有解決方案直接通過已知索引來訪問數組 – Dyna