问题
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