The components inside of your container components can easily accept Observables. You simply define your custom @Input then use the Async pipe when you pass the Observable in. This lesson walks you through the process of passing an Observable into a Component.

The idea is Your smart component prepares the data and use 'async pipe' to pass into the dumb component to display. So the dump component has no idea about Observable. Just need to display the data.

// clock.ts

import {Component, Input} from 'angular2/core';
@Component({
selector: 'clock',
template: `<h3>{{time | date:'yMMMMEEEEdjms'}}</h3>`
}) export class ClockComponent{
@Input() time;
constructor(){ }
}
// app.ts

/**
* Created by wanzhen on 26.4.2016.
*/
import {Component} from 'angular2/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/interval';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/merge';
import 'rxjs/add/operator/startWith';
import 'rxjs/add/operator/scan';
import 'rxjs/add/operator/mapTo';
import {Subject} from "rxjs/Subject";
import {Store} from '@ngrx/store';
import {SECOND, HOUR} from './reducer';
import {ClockComponent} from './clock'; @Component({
selector: 'app',
directives: [ClockComponent],
template: `
<input #inputNum type="number" value="">
<button (click)="click$.next(inputNum.value)">Update</button>
<clock [time]="time | async"></clock>
`
})
export class App {
click$ = new Subject()
.map( (number) => ({type: HOUR, payload: parseInt(number)})); seconds$ = Observable.interval()
.mapTo({type: SECOND, payload: }); time; constructor(store:Store) {
this.time = store.select('clock'); Observable.merge(
this.click$,
this.seconds$
)
.subscribe(store.dispatch.bind(store))
}
}

Here using 'store.dipatch.bind(store)' instead of 'function(action){store.dispatch(action)}'.

// reducer.ts

export const SECOND = "SECOND";
export const HOUR = "HOUR"; export const clock = (state = new Date(), {type, payload})=> {
const date = new Date(state.getTime());
switch(type){
case SECOND:
date.setSeconds(date.getSeconds() + payload);
return date; case HOUR:
date.setHours(date.getHours() + payload);
return date; } return state;
};
// main.ts

import {bootstrap} from 'angular2/platform/browser';
import {App} from './app';
import {provideStore} from '@ngrx/store';
import {clock} from './reducer'; bootstrap(App, [
provideStore({clock})
]).then(
()=> console.log('App running...'),
err=> console.log(err)
); /*
* 1. Create a reducer
* 2. Use provideStore({reducer_name}) to provide store
* 3. Use store.select('reducer_name') to get store in Observable type
* 4. Apply logic to dispatch the action
* */

[Angular 2] Passing Observables into Components with Async Pipe的更多相关文章

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

  2. [Angular 2] Passing data to components with @Input

    @Input allows you to pass data into your controller and templates through html and defining custom p ...

  3. [Angular 2] Passing data to components with 'properties'

    Besides @Input(), we can also use properties on the @Component, to pass the data. import {Component, ...

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

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

  6. [Angular 2] Passing Template Input Values to Reducers

    Angular 2 allows you to pass values from inputs simply by referencing them in the template and passi ...

  7. [Angular2 Router] Use Params from Angular 2 Routes Inside of Components

    Angular 2’s ActivatedRoute allows you to get the details of the current route into your components. ...

  8. Angular: 使用 RxJS Observables 来实现简易版的无限滚动加载指令

    我使用 angular-cli 来搭建项目. ng new infinite-scroller-poc --style=scss 项目生成好后,进入 infinite-scroller-poc 目录下 ...

  9. [Angular] Show a loading indicator in Angular using *ngIf/else, the as keyword and the async pipe

    The network may be unreliable and loading data may take time. Thus it is important to give the user ...

随机推荐

  1. Python os常用模块

    Python的标准库中的os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Wi ...

  2. eclipse/myeclipse 变量名自动补全问题

    原理是:在输入变量名后,去掉按下空格或=后,代码上屏  以前只知道alt+/调出assist,后来发现可以所有字母都激活content assist(8.1里有写).用起来果然很爽,但是eclipse ...

  3. idea sass scss配置

    1.安装Ruby win  直接http://rj.baidu.com/soft/detail/22711.html?ald mac linux https://ruby.taobao.org/ 可下 ...

  4. 记一次npapi插件无窗口(windowless )化下的妙巧思路然后解决问题的超爽体验过程

     1:问题 集成第三方的ocx控件,用来做pdf显示和签名.如果用窗口化插件做,很简单,加载ocx到窗口中,再显示到网页即可.但这样有个缺点.就是这个窗口会浮动在网页元素的上面,导致遮挡住网页元素.比 ...

  5. 你好,C++(34)有一只叫做多利的羊 6.2.4 拷贝构造函数

    6.2.4  拷贝构造函数 在C++世界中,除了需要使用构造函数直接创建一个新的对象之外,有时还需要根据已经存在的某个对象创建它的一个副本,就像那只叫做多利的羊一样,我们希望根据一只羊创建出来另外一只 ...

  6. rhel-server-6.2-i386安装gcc、g++步骤

    安装的版本:rhel-server-6.2-i386 RHEL 6.2默认是没有gcc和gcc-c++环境的,而且我也没有$购买正版服务.只能本地安装了,总结方法如下: 上传安装镜像rhel-serv ...

  7. [FindBugs分析记录]Potentially dangerous use of non-short-circuit logic

    官网解释: This code seems to be using non-short-circuit logic (e.g., & or |) rather than short-circu ...

  8. ubantu-命令-进入超级用户

    sudo su; 建立文件夹 mkdir ulike_work;

  9. git log友好显示

    查看commit 提交日志 $ git log $git log --pretty=oneline $git reflog 显示所有提交记录,包括已经回退的提交,如图:提交了abc 和 bb 然后回退 ...

  10. PHP自定义错误处理

    自定义错误报告的处理方式,可以完全绕过标准的PHP错误处理函数,这样就可以按照自己定义的格式打印错误报告,或改变错误报告打印的位置(标准PHP的错误报告是哪里发生错误就在发生位置处显示).以下几种情况 ...