Skip to content

Instantly share code, notes, and snippets.

@roelofjan-elsinga
Created July 3, 2018 19:49
Show Gist options
  • Save roelofjan-elsinga/763eddfcd6e2948b9135286081c83da3 to your computer and use it in GitHub Desktop.
Save roelofjan-elsinga/763eddfcd6e2948b9135286081c83da3 to your computer and use it in GitHub Desktop.
Subscribing to an action in Angular 6
import {Component, OnInit} from '@angular/core';
import {NgRedux, select} from "@angular-redux/store";
import {IAppState} from "./path/to/state";
@Component({
selector: 'app-any',
templateUrl: './any.component.html',
styleUrls: ['./any.component.scss']
})
export class AnyComponent implements OnInit {
@select() languages;
constructor(private ngRedux: NgRedux<IAppState>) { }
ngOnInit() {
//@angular-redux/store provides the @select decorator,
//so you can listen to a change in a specific reducer
this.languages.subscribe(language => {
console.log(languages);
//Will display: {shortcode:"en",name:"English",english_name:"English"}
});
//You can also use NgRedux to listen to the whole state for changes if you want
this.ngRedux.subscribe(() => {
let state = this.ngRedux.getState();
console.log(state.languages);
//Will also display: {shortcode:"en",name:"English",english_name:"English"}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment