[英]Angular 2 - Invalid Pipe Argument: '' for pipe 'Async Pipe'
在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);
}
}
在服务中
export class ServiceX {
constructor() {}
getJSON(filepath){
return Observable.create((observer:any) =>{
//retrieve JSON here
observer.next(jsonArr);
observer.complete();
});
}
}
我遇到此错误:管道'Async Pipe'的管道参数无效:“
您的*ngFor
引用的是可观察的,而不是其中的数据。 在ngIf
,可以正确地ngIf
数组,但是如果要使用数组中的值,则应将结果声明为变量,请执行以下操作: optionsArr | async as options
optionsArr | async as options
<-注意as
关键字。 然后继续使用子元素中的options
作为对数组的引用。 因此,您的*ngFor
将变为*ngFor="let option of options"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.