[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 ...
随机推荐
- shell编程备忘
1.脚本存放目录 workspace="$(cd "$(dirname "$0")"; pwd)" 2.输出 其中 command 代表指 ...
- mockServer学习
mockServer学习 很喜欢mockserver官方主页的背景颜色和格式 官方主页如下: http://www.mock-server.com/
- Ajax跨域请求中的Cookie问题(默认不带cookie等凭证)
1.原生Ajax请求方式,设置跨域请求附带详细参数 var xhr = new XMLHttpRequest(); xhr.open("POST", "http://xx ...
- Java中 int和Integer的区别+包装类
--今天用Integer 和Integer 比较 发现有问题,于是去查了查. 1.Java 中的数据类型分为基本数据类型和引用数据类型 int是基本数据类型,Integer是引用数据类型: Inget ...
- winform登陆 记住用户名 代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Mysql中的DQL查询语句
----------------1.查询所有列 --查询 学生 表所有记录(行) select *from 学生 --带条件的查询 select *from 学生 where 年龄>19 --- ...
- 转 常用JQuery插件整理
虽然自己也写过插件,但JQuery插件种类的繁多,大多时候,我还是使用别人写好的插件,这些都是我用了同类插件里较为不错的一些,今天就整理一下公开放出来. UI: jquery.HooRay(哈哈,自己 ...
- 【iOS问题】字典转模型,属性个数不匹配问题
一.字典转模型的键值对与模型属性不匹配问题 1. 字典的键个数 < 模型的属性个数 (key 能与模型的属性匹配) 1> .KVO 方式: - setValuesForKeysWithDi ...
- 安卓4.2原生rom状态栏显示运营商
前言:要调整状态栏布局,需反编译systemui.apk.单卡机修改status_bar.xml和signal_cluster_view.xml,双卡机修改gemini_status_bar.xml和 ...
- Javascript Promise 学习 (中)
时隔多日,对promise有了多一点点的了解. 最近用angularjs 做开发,所以研究了一下它的 $q 功能不算很强大,算是简化版的 Q.js 参考了一下源码,不过我的等级还差很多... 作为学习 ...