[Angular 2] Passing observables into components with Async Pipe

Source: Internet
Author: User

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 your through the process of passing an Observable into a Component.

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

// clock.ts  from'angular2/core'; @Component ({    '  Clock',    Template: ''ymmmmeeeedjms '}} 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="0"> <button (click) ="Click$.next (Inputnum.value)">Update</button> <clock [time]="Time | Async "></clock>         `}) ExportclassApp {click$=NewSubject (). Map ((number)=({type:hour, payload:parseint (number)})); seconds$= Observable.interval ( +). Mapto ({type:second, payload:1});    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.tsExportConstSECOND ="SECOND"; exportConstHOUR ="HOUR"; exportConstClock = (state =NewDate (), {type, payload}) = = {    ConstDate =NewDate (State.gettime ()); Switch(type) { CaseSECOND:date.setSeconds (date.getseconds ()+payload); returndate;  CaseHOUR:date.setHours (date.gethours ()+payload); returndate; }    returnState ;};

//main.tsImport {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 the components with Async Pipe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.