Skip to content

Instantly share code, notes, and snippets.

@pglazkov
Created November 20, 2017 15:40
Show Gist options
  • Save pglazkov/15f64a88e422e94114c57ce5d78bc729 to your computer and use it in GitHub Desktop.
Save pglazkov/15f64a88e422e94114c57ce5d78bc729 to your computer and use it in GitHub Desktop.
import { Component } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { SharedData } from '../shared-data';
@Component({
selector: 'app-main',
template: `
<button (click)="loadData()">Load data</button>
<div *ngIf="sharedData.value | async; let data">
<h3>Data:</h3>
<ul>
<li *ngFor="let item of data.items">{{ item }}</li>
</ul>
</div>
`
})
export class MainComponent {
constructor(public sharedData: SharedData) { }
async loadData() {
let data = await this.loadDataFromHttpService().toPromise();
this.sharedData.init(data);
}
private loadDataFromHttpService() {
// Simulate loading data from HTTP service
return Observable.of({ items: ['a', 'b', 'c']}).delay(2000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment