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. 字符串转化为json的三种方法

    1,eval方式解析,恐怕这是最早的解析方式了.如下: function strToJson(str){ var json = eval('(' + str + ')'); return json;  ...

  2. jQuery验证框架 .

          目录视图 摘要视图 订阅 “程序人生”中国软件开发者职业生涯调查     CSDN社区“三八节”特别活动      开发者职业生涯调查之未来 jQuery验证框架 分类: JQuery 2 ...

  3. Arcgis Android 基本概念 - 浅谈

    MapView     MapView 是 Android 中 ViewGroup的子类,也是 ArcGIS Runtime SDK for Android 中的地图容器,与很多 ArcGIS API ...

  4. JavaScript 函数作用域和闭包

    函数作用域和闭包  词法作用域   它们在定义它们的作用域里运行,而不是在执行的作用域运行,但是只有在运行时,作用域链中的属性才被 定义(调用对象),此时,可访问任何当前的绑定.   调用对象     ...

  5. 【基础教程】推荐10+必备的 WordPress 常用插件

    1.Akismet Akismet 是 WordPress 官方推荐的一款 WordPress 防垃圾评论插件,也是默认已安装的插件. 2.WP-Postviews 最好的最流行的WordPress浏 ...

  6. php实现获取汉字的首字母实例

    //取汉字的asc区间然后返回汉字首字母了,代码如下:header("Content-type: text/html; charset=utf-8"); function getf ...

  7. Poweroff – 很好很强大的定制关机工具

    Poweroff – 很好很强大的定制关机工具 Poweroff 是一个用来管理电脑关机系统的小工具,支持定时,支持远程 作者开放源代码,有兴趣的同学可以尝试着制作一下汉化版本.   可以设定不同时间 ...

  8. 像素转换mm

    public static double MillimetersToPixelsWidth(double length) //length是毫米,1厘米=10毫米 { System.Windows.F ...

  9. jdk8永久代从方法区移除的验证

    /*** 测试使用jdk8中是否仍然可以使用永久代* jvm options * -Xms20m -Xmx20m -Xmn10m -XX:PermSize=10m -XX:MaxPermSize=10 ...

  10. 2个Web上传组件

    http://www.uploadify.com/download/ http://gmupload.tanjun.com.cn/