在component.html中
<select name="select1" *ngIf="optionsArr | async">
<option *ngFor="let option of optionsArr">{{option}}</option>
</select>在component.ts中
export class ComponentX implements OnInit {
optionsArr = [];
constructor(private service : ServiceX) { }
ngOnInit() {
this.optionsArr = this.service.getJSON(filepath);
}
}在service.ts中
export class ServiceX {
constructor() {}
getJSON(filepath){
return Observable.create((observer:any) =>{
//retrieve JSON here
observer.next(jsonArr);
observer.complete();
});
}
}我有一个错误:无效的管道参数:'‘用于管道’异步管道‘
发布于 2018-07-09 11:15:08
您的*ngFor引用的是可观察到的数据,而不是内部的数据。在ngIf中,可以正确地传递数组,但是如果要使用数组中的值,则应该将结果声明为变量,执行以下操作:optionsArr | async as options <-注意as关键字。然后继续在子元素中使用options作为对数组的引用。所以你的*ngFor会变成*ngFor="let option of options"
https://stackoverflow.com/questions/51243900
复制相似问题