Created
April 19, 2016 18:02
-
-
Save pauleveritt/a86ccbe510bb754107354274dfae9052 to your computer and use it in GitHub Desktop.
Async pipe on object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {Component} from "angular2/core"; | |
| import {Store, Reducer} from "@ngrx/store"; | |
| import {Observable} from "rxjs/Observable"; | |
| interface AppState { | |
| counter: number; | |
| } | |
| interface ICounter { | |
| id: number, | |
| value: number | |
| } | |
| const INCREMENT = 'INCREMENT'; | |
| const counter: Reducer<ICounter> = (state: ICounter = { id: 1, value: 0 }, | |
| { type, payload }) => { | |
| switch (type) { | |
| case INCREMENT: | |
| return { id: payload.id, value: payload.value++ }; | |
| default: | |
| return state; | |
| } | |
| }; | |
| @Component({ | |
| selector: 'my-app', | |
| template: ` | |
| <span>Counter: {{ counter.value | async }}</span> | |
| <button (click)="increment()">Increment</button> | |
| ` | |
| }) | |
| export class AppComponent { | |
| counter: Observable<ICounter>; | |
| constructor(public store: Store<AppState>) { | |
| this.counter = store.select('counter'); | |
| } | |
| increment() { | |
| const payload: ICounter = { id: 1, value: 0 }; | |
| this.store.dispatch({ type: INCREMENT, payload }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment