Angular 2 Display value from Observable

瘦欲@ 提交于 2019-12-24 07:15:51

问题


Hi I want display my value from response http but I don't know what is the problem.

http :

getAllApiContext(){
     const options = this.getOptions("...");
    return this.http
        .get("jenkins/job/api_initialization/api/json", options)
        .map((data: any) => {
            return data.json().property[0].parameterDefinitions[1].choices;
        })
}

She return array.

My component.ts

context: Observable<Array<any>>;
ngOnInit() {
  this.jenkinsJob.getAllApiContext().subscribe(data => {
    this.context = data;
  });
};

My component.html

 <span *ngFor="let item of context | async">
   <p>{{item}}</p>
 </span>

回答1:


remove the async in the ngFor. since you are already subscribing to the observable, no need to use the async pipe

<span *ngFor="let item of context">



回答2:


If you want to use async pipe you don't need to subscribe

ngOnInit() {
 this.context = this.jenkinsJob.getAllApiContext();
};

AsyncPipe



来源:https://stackoverflow.com/questions/51394424/angular-2-display-value-from-observable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!