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. Jsp笔记(1)

    1. jsp页面中出现中文乱码怎么解决? <%@ page contentType="text/html; charset=GB2312"%> <%@ page ...

  2. IIS与ASP.NET 通信机制深度剖析

    IIS5.X缺点: ISAPI 动态连接库被加载到InetInfo.exe 进程中,它和工作进程之间是一种典型的跨进程通信方式,尽管采用命名管道,但是仍然会带来性能的瓶颈. 所有的 ASP.NET 应 ...

  3. XP系统取消开机硬件检查

    非正常关机后进行磁盘检查,主要用于检查磁盘错误等.非法关机后会丢失一些文件或产生一些文件错误,而硬盘自检恰恰就是来检查这些错误并对之进行必要的修复,此功能如果被关闭,不能进行必要的数据恢复,久而久之会 ...

  4. Tomcat:Can't load IA 32-bit .dll on a AMD 64-bit platform问题的解决

    控制台错误如下: java.lang.UnsatisfiedLinkError: D:\apache-tomcat-7.0.56\bin\tcnative-1.dll: Can't load IA 3 ...

  5. 磁盘阵列RAID

                                                                                  磁盘阵列RAID 年提出的.RAID名为独立 ...

  6. HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)

    以前好像是在UVa上貌似做过类似的,mod的剩余,今天比赛的时候受baofeng指点,完成了此道题 此题题意:求sum(|i%A-i%B|)(0<i<N-1) A.B的循环节不同时,会有重 ...

  7. golang 阻塞的坑

    1.写go程序的时候一定要牢记主线程不能阻塞,一旦阻塞就崩溃,就怕那种某种情况下偶尔会出现几下阻塞的,一定要杜绝.2.select case的执行顺序是乱的,不确定的3.写入管道的时候,一定要保证管道 ...

  8. idea intellij 快捷键(ubuntu版本)

    S + C + T 创建测试类 A + F12 开启终端 C + F12 查看类中的方法属性 ----随时更新,记录快捷方式

  9. JavaScript escape() 函数

    JavaScript escape() 函数 JavaScript 全局对象 定义和用法 escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串. 语法 escape(str ...

  10. javascript中的事件问题

    事件的类型: (1)鼠标事件:   click用户点击鼠标时发生,当用户的焦点在按钮上,并按了回车键,同样会触发这个事件 dbclick 用户双击鼠标左键时发生    mouseover 鼠标移出某个 ...