[Angular 2] Using Promise to Http
2024-10-13 05:55:27
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>
最新文章
- Storm的ack机制在项目应用中的坑
- Spring+SpringMvc+Mybatis整合注意事项
- TreeMap源码分析
- Python基础(5)--字典
- 盘点十大最流行的Linux服务器发行版
- iOS-Block两个界面传值
- python的一些总结2
- winform 剔除空格与换行显示
- javascript 倒计时跳转.
- log4j.properties全配置 (转)
- ARM应用调试思路、方法总结、笔记
- openstack-ocata-身份验证2
- VS2010 集成标准com组件
- Sql Server语句大全
- c# 对JSON字符串排序(KEY/VALUE)
- Linux 第八天
- PHP报错类型
- 用NDK调用第三方库
- O&#39;Reilly总裁提姆-奥莱理:什么是Web 2.0
- Android判断当前网络是否可用--示例代码