Angular 2: when I change a variable in a promise. in ngOnInit view is not updated

I created a short example of an Angular 2 application built using NativeScript , and I expect the foo variable to be "foo" at the beginning, then ngOnInit() will change it to "bar" first, and then the promise will return, and I should see " foobar "in Label, but instead I see a" bar ".

The execution is done as I see in the logs:

 JS: afterBar JS: promise new JS: promise than 

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"); }); } 

Type of application:

 <Label [text]='foo'></Label> 

How can I change the label text in a promise and see it β€œrefreshing” in the application view?

SOLVE:

I solved the problem by running the update

 import { ChangeDetectorRef } from '@angular/core'; [...] constructor( [...], private ref: ChangeDetectorRef) { } [...] this.ref.detectChanges(); 
0
angular nativescript angular-promise
source share

No one has answered this question yet.

See similar questions:

12
Angular 2 don't refresh view after clicking array in ngOnInit prom

or similar:

626
Angular / RxJs When Should I Unsubscribe from `Subscription`
360
How to determine when @Input () value changes in Angular?
318
Run change detection manually in Angular
284
How to determine the route change in the Corner?
12
Angular 2 don't refresh view after clicking array in ngOnInit prom
4
NativeScript TabView Overlay Button?
3
NativeScript cannot get or set user interface properties when loading a page
one
NativeScript debugging with Android fails
0
NativeScript 4.1 with a corner: Modal view with navigation can lead to router problems, so some pages no longer appear

All Articles