码迷,mamicode.com
首页 > Web开发 > 详细

[Angular 2] Using Promise to Http

时间:2016-04-07 06:57:38      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

You can also use Promise for http:

 

So for the service, you need to call toPromise() method:

  getVehicles(value?: string) {
    return this._http.get(‘api/vehicles.json‘)
      .map((response: Response) => <Vehicle[]>response.json().data)
      .toPromise()
      .catch(this.handleError);
  }

 

Then in your controller, you can get the Promise back:

  getHeroes(value?: string) {
    this.vehicles = this._vehicleService.getVehicles(value);
  }

But notice that, we assign the Promise to the this.vehices. so it means we use ‘async‘ pipe:

<ul>
  <li *ngFor="#vehicle of vehicles | async"
    (click)="select(vehicle)">
    {{ vehicle.name }}
  </li>
</ul>

[Angular 2] Using Promise to Http

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5361936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!