0

I have some object in my Component:

this.user = Authentication.user;

Which works just fine - it copies the reference, and if Authentication.user changes, this.user in my Component changes as well.

However, I am wondering, if it is possible to do the following:

this.user = Authentication.getUser()

where Authentication.getUser :

getUser(){
    return this.user;
}

However, this does not seem to copy reference to user from Authentication.

Am I doing something wrong here, or it not possible?

UPDATE

Actually, it works pretty nice:

http://plnkr.co/edit/ULuSl6CIBCzi7gcsLu3R?p=preview

3
  • Hard to tell. It's not obvious what Authentication.user is, and where it comes from. " if Authentication.user changes, this.user in my Component changes as well." probably means when a property of Authentication.user changes, otherwise this would not be the case. Please try to create a Plunker that demonstrates what you try to accomplish. Commented Mar 11, 2016 at 9:20
  • @GünterZöchbauer, sure thing, i will do it in few mins! Commented Mar 11, 2016 at 9:20
  • @GünterZöchbauer, it actually works, but doesnt work with primitives. Do you know if Angular2 fixed issue with binding to a primitive? Commented Mar 11, 2016 at 9:28

1 Answer 1

1

There is no way to fix this for primitives.

TypeScript transpiles to JavaScript and you can't change how JavaScript handles primitive types.

If you want to synchronize, create an Observable that sends value changes, then the receiver can subscribe and gets notified about changes and update his copy of the value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.