[Angular 2] Async Http
Async Pipe:
The Asynce pipe receive a Promise or Observable as input and subscribes to the input, evetually emitting the value(s) changes arrive.
In the demo, the logic is fom the list component, we ask service to get Heros by calling Start War API, on the service side, we only return array of heros with observalbe type:
getHeros(){
return this._http.get('http://swapi.co/api/people')
.map( (res: Response) => <Hero[]>res.json().results)
.catch(this.handleError);
}
...
export class Hero{
constructor(public name: string){}
}
Here <Hero[]>, we use Typescript to convert the raw json data to Hero[]. The same as you new Hero().
For the list, we assign the return value from service to this.heros , so it is a list of Hero with Observable type, then we apply async pipe to the html.
heros: Observable<Hero[]>;
getHeros(){
this.heros = this.heroService.getHeros();
}
<ul>
<li *ngFor="#hero of heros | async">
{{hero.name}}
</li>
</ul>
[Angular 2] Async Http的更多相关文章
- [Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword
Angular allows us to conveniently use the async pipe to automatically register to RxJS observables a ...
- [Angular] New async 'as' syntax and ngIf.. else
From Anuglar v4 above, we are able to using 'as' with async pipe. This allow as using 'new variable' ...
- angular学习笔记(二十九)-$q服务
angular中的$q是用来处理异步的(主要当然是http交互啦~). $q采用的是promise式的异步编程.什么是promise异步编程呢? 异步编程最重要的核心就是回调,因为有回调函数,所以才构 ...
- Working with Validators and Messages in AngularJS
原文:http://odetocode.com/blogs/scott/archive/2014/10/16/working-with-validators-and-messages-in-angul ...
- [Angular 2] Handle Reactive Async opreations in Service
When you use ngrx/store and you want to fire a service request. When it sucessfully return the respo ...
- [Angular 2] Rendering an Observable with the Async Pipe
Angular 2 templates use a special Async pipe to be able to render out Observables. This lesson cover ...
- [Angular 2] Controlling Rx Subscriptions with Async Pipe and BehaviorSubjects
Each time you use the Async Pipe, you create a new subscription to the stream in the template. This ...
- [Angular 2] Passing Observables into Components with Async Pipe
The components inside of your container components can easily accept Observables. You simply define ...
- [Angular 2] Rendering an Observable Date with the Async and Date Pipes
Instead of simply pushing numbers on a timer into the template, now we'll move on to pushing actual ...
随机推荐
- Web Api学习一
接触WebApi读的第一篇文章: ASP.NET Web API(一):使用初探,GET和POST数据 实践过程中,用的Fiddler模拟Post请求时收到的对象总是为空null 解决:将文章中的内容 ...
- android 广播分类
安卓广播分为两类:1.普通广播, broadcast,广播发出之后所有满足条件的应用都能获取到广播里面的数据,缺点是应用获取广播中的数据修改之后不能传递给其它接收广播的应用:2.有序广播,orderb ...
- UGUI之UI的深度问题
学过NGUI的都知道,NGUI的深度是通过值来控制的.Panel也是UI也是,如果空间太多,布局复杂UI深度的值会变得很混乱.所以在NGUI中设置UI深度时一定要多加思考.然而在UGUI控制显示顺序的 ...
- 武汉科技大学ACM:1007: 陶陶摘苹果
Problem Description 厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳上再试试. 个苹果到地面的高度,以及陶陶把手伸直的时候能够达到的最大高度,请帮陶陶算一下她能够摘到的苹 ...
- uva 12207 - That is Your Queue
#include <cstdio> #include <iostream> #include <deque> using namespace std; int ma ...
- HTTP状态码:400\500 错误代码
一些常见的状态码为: 200 - 服务器成功返回网页404 - 请求的网页不存在503 - 服务不可用详细分解: 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明100 ...
- 4种CSS3效果(360度旋转、旋转放大、放大、移动)
旋转: * { transition:All .4s ease-in-out; -webkit-transition:All .4s ease-in-out; -moz-transition:All ...
- acdream暴力专场中的优美暴力
F - 小晴天老师系列——苹果大丰收 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...
- java-map-IdentityHashMap
1.背景 今天翻开IdentityHashMap的时候,就傻眼了,这个到底是个逻辑啊,我的程序代码如下: IdentityHashMap<String,String> identityHa ...
- eclipse中myBatis引入
1.添加config.xml配置文件 2.定义与数据库的数据实体映射类 3.创建操作表的是sql映射文件 即:mapper.xml 4.在配置文件config.xml中注册sql映射文件(步骤三创建的 ...