subscribe in Angular 2 -


i want ngoninit function next things: - make http request data using this.structurerequest.sendrequest(),which works fine, , after data have been received start view using this.viewnodes() function. use subscribe , not work, think wrong subscribe function. please help:)

  1. homecomponent.ts

    import {component} '@angular/core'; import {observable} 'rxjs/observable'; import {structurerequestservice} './structurerequestservice';   export class content {   ok: boolean; content = []; } @component({ providers: [structurerequestservice], styleurls: ['app/home/home.css'], templateurl:'./app/home/homepagetemplate.html'  })  export class homecomponent { contentarray = []; myres: content; showassigned:boolean = false; showsubitems:boolean = false; showusers:boolean = false;  constructor(private structurerequest: structurerequestservice) {}  ngoninit() { this.structurerequest.sendrequest().subscribe( this.viewnodes());  }  viewnodes() { this.myres = this.structurerequest.result; this.contentarray = this.myres.content; this.showassigned = true;  }  } 

2.here http service, http works fine, data received:

import {injectable} '@angular/core'; import {http, response, headers, requestoptions} '@angular/http';  import {observable} 'rxjs/observable';  @injectable () export class structurerequestservice { result: object; //private myurl = 'http://manny.herokuapp.com/audit/get/structure'; private myurl = './app/home/nodes.json';  // local url structure api constructor (private http: http) {     //use xhr object     let _build = (<any> http)._backend._browserxhr.build;     (<any> http)._backend._browserxhr.build = () => {         let _xhr =  _build();         _xhr.withcredentials = true;         return _xhr;     }; }  sendrequest() {     let body = json.stringify({});     let headers = new headers({ 'content-type': 'application/json'});     let options = new requestoptions({         headers: headers     });      this.http.get(this.myurl, options)         .map((res: response) => res.json())         .subscribe(res => {this.result = res;             return this.result; }); } } 

3.so problem make synchronous steps: receive data, view it.

you want execute this.viewnodes when request returns value , not execute result of this.viewnodes()

this

this.structurerequest.sendrequest().subscribe( this.viewnodes()); 

should changed to

this.structurerequest.sendrequest().subscribe(() => this.viewnodes()); 

the former executs this.viewnodes() , passes result subscribe(), later creates new inline function passed subscribe(). inline function, when called, executes this.viewnodes()

if want pass value sendrequest() returns should be

this.structurerequest.sendrequest().subscribe((result) => this.viewnodes(result)); 

update

sendreqeust() doesn't return anything.

it should

return this.http.get(this.myurl, options) ... 

but works way use in code if returns observable.

but subscribe() @ end returns subscription

 return this.http.get(this.myurl, options)     .map((res: response) => res.json())     .subscribe(res => {this.result = res;         return this.result; }); 

therefore should changed to

 return this.http.get(this.myurl, options)     .map((res: response) => res.json())     .map(res => {       this.result = res;       return this.result;      }); 

or

 return this.http.get(this.myurl, options)     .map((res: response) => res.json())     .do(res => {       this.result = res;     }); 

the difference do() doesn't modify value of stream , doesn't need return anything. value returned .map() forwarded. ensure do imported (like map) if want use it.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -