17.10.2018 Views

Angular

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

export interface IAppState {<br />

example?: string;<br />

}<br />

export const INITIAL_STATE: IAppState = {<br />

example: null,<br />

};<br />

export function rootReducer(state: IAppState = INITIAL_STATE, action: Action): IAppState {<br />

switch (action.type) {<br />

case EXAMPLE_CHANGED:<br />

return Object.assign(state, state, (action));<br />

default:<br />

return state;<br />

}<br />

}<br />

actions.ts<br />

import {Action} from "redux";<br />

export const EXAMPLE_CHANGED = 'EXAMPLE CHANGED';<br />

export interface UpdateAction extends Action {<br />

example: string;<br />

}<br />

Section 63.2: Get current state<br />

import * as Redux from 'redux';<br />

import {Inject, Injectable} from '@angular/core';<br />

@Injectable()<br />

export class exampleService {<br />

constructor(@Inject(AppStore) private store: Redux.Store) {}<br />

getExampleState(){<br />

console.log(this.store.getState().example);<br />

}<br />

}<br />

Section 63.3: change state<br />

import * as Redux from 'redux';<br />

import {Inject, Injectable} from '@angular/core';<br />

@Injectable()<br />

export class exampleService {<br />

constructor(@Inject(AppStore) private store: Redux.Store) {}<br />

setExampleState(){<br />

this.store.dispatch(updateExample("new value"));<br />

}<br />

}<br />

actions.ts<br />

export interface UpdateExapleAction extends Action {<br />

GoalKicker.com – <strong>Angular</strong> 2 Notes for Professionals 201

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!