Angular 2: when i change a variable in a promise.than in ngOnInit the view doesn't refresh -
i created short example of angular 2 application built nativescript
, expect variable foo
"foo" @ beginning, ngoninit()
first change "bar" , promise return , should see "foobar" in label, instead see "bar".
the execution expected can see in logs:
js: afterbar js: promise new js: promise
component.ts
foo:string="foo"; ngoninit() { this.foo = "bar"; console.log("afterbar"); var promise = new promise((resolve)=>{ resolve(42); console.log("promise new"); }); promise.then(x=> { this.foo="foobar"; console.log("promise than"); }); }
the app view:
<label [text]='foo'></label>
how can change label text in promise see "refreshing" app view?
solved:
i solved problem forcing update
import { changedetectorref } '@angular/core'; [...] constructor( [...], private ref: changedetectorref) { } [...] this.ref.detectchanges();
Comments
Post a Comment