角2显示值来自可观察

时间:2018-07-18 05:50:26

标签: angular observable

嗨,我想显示来自响应http的值,但我不知道出了什么问题。

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;
        })
}

她返回数组。

我的component.ts

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

我的component.html

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

2 个答案:

答案 0 :(得分:4)

<div class="row" data-equalizer> <div class="large-5 small-12 columns" data-equalizer-watch> <img class="show-for-medium-up" src="~/Content/Images/BallCropped-800.jpg" /> </div> <div class="large-4 small-12 columns" data-equalizer-watch> <p class="subhead">Middle Text</p> <ul> <li> One </li> <li>Two</li> <li>Three</li> </ul> </div> <div class="large-3 small-12 columns " data-equalizer-watch> <img src="~/Content/Images/Bottomlogo-400.gif" /> </div> 中删除异步。由于您已经订阅了Observable,因此无需使用异步管道

ngFor

答案 1 :(得分:3)

如果您想使用async管道,则无需订阅

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

AsyncPipe