Node.js + Angular 2 RxJS Observabes & Change Detection - Best way to utilize Ajax Service with REST API? -
i new angular 2 , not know rxjs or nature of observables. trying achieve functionality however, , looking guide right information should learning achieve , more thorough knowledge.
i working on basic web application more of test anything, several of angular 2 routed views need asynchronously getting data rest api endpoint built in node.js.
here trying achieve:
when route (with angular) site.com/messages
there view, component utilizing service pulled in through parent component's providers array. need service data api endpoint site.com/api/messages
.
it seems me intuitive way have the component needs data setup http.get()
on setinterval()
when component initialized (ngoninit
life cycle hook). repeatedly poll endpoint need, gets data view can use structural directive iterate through or that.
issue feel primitive way handle getting data. using newest , effective , efficient way this. unfortunately new angular 2 , not familiar of technologies , practices behind it. have heard of , seen rxjs observables, angular change detection, rxjs observable streams, , wondering best way learning these , buy me far asynchronous data fetching angular 2 service. information how utilize these technologies , how fit great. thanks
you should use interval method of observable class. example:
constructor() { initializepolling().subscribe( (data) => { this.datatodisplay = data; } ); } initializepolling() { return observable .interval(60000) .flatmap(() => this.getnewmessages()); }
you can use datatodisplay in component template directly.
you can leverage async pipe display observable:
constructor() { this.datatodisplay = initializepolling(); }
and in template example:
<div *ngfor="d of datatodisplay | async">{{d.someproperty}}</div>
Comments
Post a Comment