json - Angular 2 method is not subscribing to the observable -
i'm trying json data using service class. service class
import { injectable } from '@angular/core'; import { http, httpmodule, response } from '@angular/http'; import { generaltab } from './generaltab'; import 'rxjs/rx'; import { observable } from 'rxjs/observable'; @injectable() export class generaltabservice { constructor(private _http : http) { console.log("http call"); } getstatus(): observable<any> { return this._http.get('http://samd.server.lan.at/taskmanagement/rest/taskconfigs/ind?language=en&access_token=200') .map((res:response) => <generaltab[]>res.json()) .do(data => console.log("all: " + json.stringify(data))) .catch(this.handleerror); } private handleerror (error: any) { let errmsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statustext}` : 'server error'; return observable.throw(errmsg); } }
component class
import { component, oninit } from '@angular/core'; import {generaltab} from "./generaltab"; import {generaltabservice} from "./generaltab.service"; import { observable } from 'rxjs/observable'; @component({ selector: 'general-tab', templateurl: '/general.component.html', providers : [generaltabservice] }) export class generalcomponent implements oninit{ title = 'serving data general component'; errormessage: any; status: generaltab []; mode = 'observable'; constructor (private generalservice: generaltabservice) { this.status = []; } ngoninit() { console.log("started"); this.generalservice.getstatus().subscribe( (status: any) => this.status = status, (error: any) => this.errormessage = error ); console.log(this.status); } }
generaltab class
export class generaltab { constructor(public recipientid : string, public recipientname: string, public recipientfullname: string, public ouid:string, public ouname:string, public institute:number, public shortname:string, public status:string) { } }
i see in console .do(data => console.log("all:" + json.stringify(data))) getting me json data
{"subjectsconfig":[{"subject":"client data maintenance","preselected":false,"initialduedate":"2016-11-24","actionconfigs":[{"action":"send","recipients":[{"user":{"recipientid":"bd27a4f5923fca13","recipientname":"abtabt","recipientfullname":"abtabt","ouid":"bd27a4f5923fca13","ouname":"0015",....
subscribe not assigning data status array , i'm getting blank "status" array. want data array in status variable testing purpose. how get?
if log response
console.log("started"); this.generalservice.getstatus().subscribe( (status: any) => this.status = status, (error: any) => this.errormessage = error ); console.log(this.status);
of course see blank status since above code(getstatus().subscribe
) async. meaning making request waiting response inside subscribe
, when comes, assign status
this.status
. while waiting logging(this.status
) blank.
instead try , check log,
console.log("started"); this.generalservice.getstatus().subscribe( (status: any) => { this.status = status; console.log(this.status); }, (error: any) => this.errormessage = error );
Comments
Post a Comment