Just can't seem to wrap my head around $scope variables and how they work!

I am having a hard time understanding $scope variables. I have an input field with ng-model='firstName" and a $scope variable of $scope.firstName. In my controller I set $scope.firstName = $rootScope.currentUser.firstName.

My problem is that if the user changes firstName in the input field, not only does $scope.firstName change but it also changes $rootScope.currentUser.firstName.

How can I keep this from happening? I only want to update $rootScope.currentUser.firstName if the user clicks the SAVE button.

Really! No help? Anyone?

Can you please provide sample code on plunker ?

Like @yurinondual said, post your code. It’s the only way we can help!

Do not use rootscope to store your main application logic variables. Make a “factory” and inject it in your controlers : where you need to have access to them.

Try not to use rootscope if possible.

Also, just saying, this is a basic angular functionnality question. nothing wrong with asking about this here on the ionic forum but you might get a better example/tutorial on how to use scope and how they work on an ‘angular’ message board or forum … Hope this helps :slight_smile:

1 Like

Actually, I have a factory as well for some other data and the same thing happens. It will actually change the value of the data stored in the factory. As for using $rootScope, regardless of the issue of wheather or not to use it, $scope still chnages the values unexpectedly.

I will do my best to create a demo since i seem to be the only one in the world who has experienced this!

Sorry for the sarcasm, just frustrated with this, especially since I know I can do exactly what I want using jquery without this issue!

Thanks,

RGecy

…then you’re doing it wrong.

In your controller, your doing $scope.firstName = MyFactory.firstname;

which is wrong.

just do $scope.firstName = {} or $scope.firstName = “”

Then, if the guy presses “SAVE” then you do MyFactory.firstname = $scope.firstName; so that it is saved in your factory where the permanent states for your app are saved. TADA!