我有一个可观察的字符串数组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