[Angular 2] Using Promise to Http
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的更多相关文章
- angular中的promise
angular中的promise用法 标签(空格分隔): angular 前言 Promise其实是一个规范,用类似then().then()这样的链式调用形式来处理因为异步带来意大利面条式的代码(多 ...
- Angular JS中 Promise用法
一.Promise形象讲解A promise不是angular首创的,作为一种编程模式,它出现在1976年,比js还要古老得多.promise全称是 Futures and promises. 而在j ...
- angular中使用promise
promise是一种用异步的方式处理值的方法,promise是对象,代表了一个函数最终可能的返回值或者抛出的异常,在与远程对象打交道时我们可以把他看作是远程对象的一个代理. 如果说是promise也是 ...
- 浅谈angular中的promise
promise目的就是为了跳出回调地狱.老掉牙的东西,大神轻拍. 举个最简单的例子:请求数据(getData),解析数据(executeData),显示数据(showData). //获取数据 fun ...
- 深入理解jQuery、Angular、node中的Promise
最初遇到Promise是在jQuery中,在jQuery1.5版本中引入了Deferred Object,这个异步队列模块用于实现异步任务和回调函数的解耦.为ajax模块.队列模块.ready事件提供 ...
- angular的异步处理$q的使用(promise)
Angular中的promise: Promise是一种异步方式处理值的方法.代表了一个函数最 终可能的返回值或者抛出的异常 在之前,通常都是使用闭包或者回调来响应非同步的有意义数据 使用promis ...
- angular promise $q 异步调用
Angular异步调用 Promise和$q的用法 背景 首先说明一下promise异步调用出现的背景: javascript语言是一种单线程模式,就是说一次只能够执行一个任务,如果有多个任务的话就必 ...
- [AngularJS] AngularJS系列(7) 进阶篇之promise
目录 使用promise 补充说明 $q.all $q.when 在上节中,我们在http中使用了then 和 在ngResource中返回了一个'延迟对象'. 本节介绍一下angular中的prom ...
- Angularjs中的promise
promise 是一种用异步方式处理值的方法,promise是对象,代表了一个函数最终可能的返回值或抛出的异常.在与远程对象打交道非常有用,可以把它们看成一个远程对象的代理. 要在Angular中创建 ...
随机推荐
- mysql存储过程controller的not found造成混乱的解决办法
打草稿 问题:对于select var1 into var2 ,单select的结果为空的时候(空的意思是没有找到数据,不是该值为null) ,会触发controller的not found 当游标 ...
- php获取mac用于网站绑定服务器
php获取mac用于网站绑定服务器 <?php class GetMacAddr{ var $return_array = array(); // 返回带有MAC地址的字串数组 var $mac ...
- C/C++中的&&和||运算符
代码: #include <iostream> #include <cstdio> using namespace std; int main(){ ,b=,c=-,d=; d ...
- [HDU1017]Exact cover[DLX][Dancing Links详解][注释例程学习法]
Dancing Links解决Exact Cover问题. 用到了循环双向十字链表. dfs. 论文一知半解地看了一遍,搜出一篇AC的源码,用注释的方法帮助理解. HIT ACM 感谢源码po主.链接 ...
- 浏览器d判断
1.判断浏览器类型 if navigator.userAgent.indexOf(”MSIE”)>0) {} //判断是否IE浏览器 if(isFirefox=navigator.userAge ...
- Java编程之字符集问题研究
1. 概述 本文主要包括以下几个方面:编码基本知识,java,系统软件,url,工具软件等. 在下面的描述中,将以"中文"两个字为例,经查表可以知道其GB2312编码是" ...
- python: list[-1] 与 list[-1:] 的区别
>>> l '3.542485\t1.977398\t-1\r\n' >>> l.split() ['3.542485', '1.977398', '-1'] &g ...
- Git 初始化配置
先给大家推荐个很不错的GIT学习资料:廖雪峰 <Git简介> http://www.liaoxuefeng.com/wiki/0013739516305929606dd183612485 ...
- Solr Update备注
参考资料: http://wiki.apache.org/solr/ExtractingRequestHandler#Sending_documents_to_Solr /update 标准的upda ...
- Visual Studio新建的源文件的默认编码
原来VS新建的源文件默认的编码是根据系统locale选择的.我的是国标2312.我草.可坑死我了.一直不知道. 当时主要是需要用doxygen生成html文档,它默认的输入文件的格式是UTF-8,是不 ...