Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share|improve this question
    
I think you try to give the same reference again, thats why it is not changing... try to clone/copy the loginInformtaion. – eesdil Mar 14 '16 at 21:29
    
this._LoginInformation or this.loginInformation, which is it? How does fetch() work? Is it called inside the Angular zone? If not, try adding ApplicationRef.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

You don't need to use self.userName, you should be able to use this.userName since you are in an arrow function:

this._AppPreferences.fetch(
    (value) => {
        this.userName = "User4";
    },
    (error) => {
        alert("Error loading Configuration: " + error);
    }
);
share|improve this answer

Don't instantiate the service by yourself. You might end up having several instances instead of a singleton. Use NG2's dependency injection to do that automagically for you!

import { Injectable } from '@angular/core';
@Injectable()
export class LoginInformation {

    private _UserName: string;

    public get userName(): string {
        return this._UserName
    }

    public set userName(v: string) {
        this._UserName = v;
    }
}



import { Component }from '@angular/core';
import { LoginInformation } from './loginInformation.service';

@Component()
export class YourApp {
   constructor(_loginInformation: LoginInformation){

   }

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.