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. 数据库、C#、Java生成唯一GUID 方法

    GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值.GUID ...

  2. Jquery中dialog属性小记

    代码如下: $('#dialogDiv').dialog( { hide:true, //点击关闭是隐藏,如果不加这项,关闭弹窗后再点就会出错. autoOpen:false, height:380, ...

  3. Struts2 框架验证

    struts2框架验证(xml方式):    * 首先要从页面中获取对应的标签name属性的值,在动作类action中声明同名的属性,提供get和set方法        * 创建一个xml格式验证文 ...

  4. 迭代器(iterator) 与 traits 编程技法

    看了候哥的<STL源码剖析>的迭代器那一章,在这里将思路稍微疏理一下 迭代器 迭代器模式的定义:提供一种方法,在不需要暴露某个容器的内部表现形式情况下,使之能依次访问该容器中的各个元素. ...

  5. 画板社交工具开发分享——HTML5 canvas控件、PHP、社交分享学习(一)

    不谈国内社交网站(人人.微博等)目前的盈利状况如何,facebook.twitter都已经取得了很大的成功.这一定程度上都得益于人们对社交的喜爱和投入. 目前的情况来看,也有很多人已经对直接的文字分享 ...

  6. PHP连续签到

    require "./global.php"; $act = isset($_GET['act']) ? $_GET['act'] : "error"; // ...

  7. cxf所用的lib

    cxf_lib

  8. Android Init语言

    Android Init语言是一种特别简单的语言,专门用来写Android的Init进程使用的配置文件的. 相当于Linux系统中的rc文件(这句话对于Linux者多半是句废话). Android I ...

  9. Python----Tornado安装

    Tornado安装,环境准备:          1.python安装包及安装 2.Tornado安装包 Python包安装 Linux下安装 如果使用的是 Linux系统 或 Mac OS X ,系 ...

  10. sql对日期的处理,一个存储过程示例

    IF v_docType = 3 THEN update T_PATIENT_INFO set USER_NAME =userName ,SEX = v_sex,BIRTHDAY = to_date( ...