I created a simple class in TypeScript:
export class LoginInformation {
private _UserName: string;
public get userName(): string {
return this._UserName
}
public set userName(v: string) {
this._UserName = v;
}
}
Then i can instantiate the class:
private _LoginInformation: LoginInformation;
this._LoginInformation = new LoginInformation();
(and also implement the getter and setter), then assign a value
this.loginInformation.userName = "User1";
Now I can use the Object in my HTML:
<ion-item>
<ion-input type="text" placeholder="Name" [(ngModel)]="loginInformation.userName"></ion-input>
</ion-item>
Now i can change my Object-Property
this.loginInformation.userName = "User2";
and the screen is updated in the expected way. Even if i set:
var self: LoginInformation = this.loginInformation;
self.userName = "User3";
everything is OK. But if I use an async function (e.g. get a value from the app preferences - plugin)
this._AppPreferences.fetch(
(value) => {
self.userName = "User4";
},
(error) => {
alert("Error loading Configuration: " + error);
},
"LoginInformation");
the on-screen value is not updated. I thought that the assignment of the reference
self: LoginInformation = this.loginInformation
Should work in the expected way. But it seems that something is missing.
Any ideas what I'm doing wrong?
this._LoginInformation
orthis.loginInformation
, which is it? How doesfetch()
work? Is it called inside the Angular zone? If not, try addingApplicationRef.tick()
, angular.io/docs/ts/latest/api/core/ApplicationRef-class.html, to force change detection to run. – Mark Rajcok Mar 15 '16 at 15:28