Skip to content

Instantly share code, notes, and snippets.

@vvscode
Created February 8, 2019 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvscode/c2dba4384244345b18f794a39cb7354c to your computer and use it in GitHub Desktop.
Save vvscode/c2dba4384244345b18f794a39cb7354c to your computer and use it in GitHub Desktop.
handle-api-call-state-nicely
@Component({
selector: 'app-news',
template: `
<button (click)="load()">Load News</button>
<!--loading spinner-->
<p *ngIf="(news$ | async).loading">loading...</p>
<p *ngFor="let item of (news$ | async).entities">{{item}}</p>
`
})
export class NewsComponent {
news$: Observable<News>;
constructor(private store: Store<{news: News}>) {
this.news$ = this.store.select(state => state.news);
}
load() {
this.store.dispatch(new LoadNews());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment