angularjs - Angular 2 - http get return wrong object -


i'm using @angular/http fetch data server, here code :

private _currentpt: any; public phongtrodetailchange = new subject();  layphongtro(id: number): promise<any> { return new promise((resolve, reject) => {   this.http     .get(constants.apiurl + 'phongtro/' + id, { headers: constants.headers })     .map((resp: response) => resp.json())     .subscribe(resp => {       console.log(resp);       // if (!resp.result) {       //   this._currentpt = resp;       //   this.phongtrodetailchange.next(true);       //   resolve(resp);       // } else {       //   this.handleerror('layphongtro', resp.result);       //   reject(resp.result);       // }     },     error => this.handleerror('layphongtro', error)); }); } 

when commented code that, property "tiencoc" of resp has right value means value 0, here picture of console.log

https://i.stack.imgur.com/7oygw.png

but when uncommented, value of "tiencoc" same property "giatien", value 1000000, here picture of console.log when uncommented

https://i.stack.imgur.com/rgxdi.png

i dont know why ? pls me, thank much

p/s: have tested postman , resp ok, means "tiencoc"'s value 0

here's fiddle demo: https://jsfiddle.net/lg6l8n2m/ (guess value before @ log :))

as i've mentioned comment when json.stringify response object printing value (because strings immutable in js). without stringify, console.log works "asynchronously", console.log prints latest value object has.

so changing tientoc 1000000 somewhere in code after console.log. , console.log prints latest value. see might in block subscribe subject phongtrodetailchange

i suggest reading immutability in javascript problem.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -