简体   繁体   English

角度2-无效的管道参数:管道'Async Pipe'的管道

[英]Angular 2 - Invalid Pipe Argument: '' for pipe 'Async Pipe'

In component.html 在component.html中

<select name="select1" *ngIf="optionsArr | async">
    <option *ngFor="let option of optionsArr">{{option}}</option>
</select>

In component.ts 在component.ts中

export class ComponentX implements OnInit {
optionsArr = [];

constructor(private service : ServiceX) { }

ngOnInit() { 
   this.optionsArr = this.service.getJSON(filepath);
}

}

In service.ts 在服务中

export class ServiceX {
   constructor() {}
   getJSON(filepath){
      return Observable.create((observer:any) =>{
          //retrieve JSON here
          observer.next(jsonArr);
          observer.complete();
      });
   }
}

I have this error: Invalid Pipe Argument: '' for pipe 'Async Pipe' 我遇到此错误:管道'Async Pipe'的管道参数无效:“

Your *ngFor references the observable and not the data within. 您的*ngFor引用的是可观察的,而不是其中的数据。 In your ngIf , you pipe the array correctly, but if you want to use the values from it you should declare the result as a variable, do this: optionsArr | async as options ngIf ,可以正确地ngIf数组,但是如果要使用数组中的值,则应将结果声明为变量,请执行以下操作: optionsArr | async as options optionsArr | async as options <- Note the as keyword. optionsArr | async as options <-注意as关键字。 Then proceed to use options in the child elements as a reference to the array. 然后继续使用子元素中的options作为对数组的引用。 So your *ngFor would become *ngFor="let option of options" 因此,您的*ngFor将变为*ngFor="let option of options"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM