Unular Corner Equivalent Variables

Working with a form for the user to edit their data based on a variable on $ rootScope (also could be $ scope).

$rootScope.formData = $rootScope.user; 

There is an ng model in the input view:

  ng-model="formData.email" 

The behavior I expect is to update the model, and only $ rootScope.formData will be updated, but instead both updates.

Is there a way to break the connection between the two?

+6
source share
2 answers

As discussed in the comments, we work with JS links. This means that we pass user as a link to another (root) area

We can call angular.copy() or cloneDeep() (see lo-dash ) to work with a new instance

+6
source

The problem is that you installed one object in another. Objects are passed by reference in Javascript, so you essentially made $rootScope.formData pointer to $rootScope.user . This is why the update updates another.

You could $rootScope.user = null; that would substantially eliminate the link, but you lost the link that it had in the first place. If this is undesirable, I think you can clone the user object in formData , which can be a feat in itself if you don't have a library that provides this feature.

+1
source

All Articles