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的更多相关文章

  1. [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 ...

  2. [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' ...

  3. angular学习笔记(二十九)-$q服务

    angular中的$q是用来处理异步的(主要当然是http交互啦~). $q采用的是promise式的异步编程.什么是promise异步编程呢? 异步编程最重要的核心就是回调,因为有回调函数,所以才构 ...

  4. Working with Validators and Messages in AngularJS

    原文:http://odetocode.com/blogs/scott/archive/2014/10/16/working-with-validators-and-messages-in-angul ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [Angular 2] Passing Observables into Components with Async Pipe

    The components inside of your container components can easily accept Observables. You simply define ...

  9. [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 ...

随机推荐

  1. HTML中<b>标签和<strong>便签的区别

    最近碰到的问题,自己写的时候因为<b>标签比较简短偶尔使用,看到别人有使用<strong>标签的,本人不懂区别,在网上找的别人的东西,觉得很有道理,跟大家分享看看~~ 链接:h ...

  2. mongodb查询只显示指定字段

    db.COMMODITY_COMMODITY.find( { "areaCode" : "320100" , "backCatalogId" ...

  3. Ajax跨域请求中的Cookie问题(默认不带cookie等凭证)

    1.原生Ajax请求方式,设置跨域请求附带详细参数 var xhr = new XMLHttpRequest(); xhr.open("POST", "http://xx ...

  4. request.ServerVariables获取环境变量

    Request.ServerVariables("HTTP_X_FORWARDED_FOR")  透过代理服务器取得客户端的真实IP地址,有些用此方法读取到的仍然是代理服务器的IP ...

  5. sql设置事务隔离级别

    SET TRANSACTION一共有以下几种级别: SET TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPE ...

  6. Android学习手记(1) Activity跳转

    新建Project,并将主页命名为MainActivity. 创建一个Activity 在App上“右键->New->Activity->Empty Activity”, 将新建的A ...

  7. hdu 1829 基础并查集,查同性恋

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  8. 一句JS搞定只允许输入数字和字母

    一句JS搞定输入框只允许用户输入数字和字母类型的内容,对象是input输入框,当然也可以其它对象,只不过input输入框用的频率非常高.一句代码,不信么?那就看下边代码: <INPUT clas ...

  9. PHP iconv()编码转换函数用法示例

    PHP iconv()字符编码转换函数的用法,iconv()函数,在php5中是内置的,语法格式:iconv("UTF- 8","GB2312//IGNORE" ...

  10. Python自动化运维之4、格式化输出、文件对象

    Python格式化输出: Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[P ...