Skip to content

Instantly share code, notes, and snippets.

@wtfuii
Created October 7, 2016 13:25
Show Gist options
  • Save wtfuii/61aa53b09dc4119dc6bcb3b0d617f03c to your computer and use it in GitHub Desktop.
Save wtfuii/61aa53b09dc4119dc6bcb3b0d617f03c to your computer and use it in GitHub Desktop.
Sharing state between Angular 2 components
@Injectable()
export class AppStateService {
constructor() {
this.appState = new Subject<AppState>();
}
state: AppState = new AppState();
appState: Subject<AppState>;
toggleMiddleBar(toggle?: boolean) {
this.state.middleBarInUse = toggle ? toggle : !this.state.middleBarInUse;
this.setState(this.state);
}
authorize() {
this.state.authorized = true;
this.setState(this.state);
}
deauthorize() {
this.state.authorized = false;
this.setState(this.state);
}
private setState(state: AppState) {
this.appState.next(state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment