Transfer from ST.GG
The difference between copy assignment and = assignment in Angular.js
Why use $scope.user = $scope.master;
$scope. Master will follow $scope. User change? What is the difference between the angular.copy and the = number assignment? Novice still do not understand, ask you.
<! DOCTYPE Html>"Http://apps.bdimg.com/libs/angular.js/1.3.9/angular.min.js"></script><body><div ng-app="myApp"Ng-controller="Formctrl"> <form novalidate>First Name:<br> <input type="text"Ng-model="User.firstname"><br>Last Name:<br> <input type="text"Ng-model="User.lastname"> <br><br> <button ng-click="Reset ()">RESET</button> </form> <p>form = {{User}}</p> <p>master = {{master}}</p></ Div><script>varApp = Angular.module ('myApp', []); App.controller ('Formctrl', Function ($scope) {$scope. master= {firstName:"John", LastName:"Doe"}; $scope. Reset=function () {$scope. User=angular.copy ($scope. master); //why use $scope. user = $scope. master; Master follows user change }; $scope. Reset ();});</script></body>You can understand this:
There's an address in memory that stores { firstName: "John", lastName: "Doe" }
this data, and it starts with $scope.master
pointing to it.
When you do $scope.user = $scope.master
, the fact is that they point to the data at the same time, so one changes and the other changes.
When you $scope.user = angular.copy($scope.master)
do, make a copy of the data (that is, the memory of the other data, the value is the same, but the address must be different), and then point to the $scope.user
copy, so they point to a different memory address, of course, does not affect each other.
Remember, this is not a special case of Angular, but rather a common basis for programming languages.
is actually a copy of the data, one is a copy reference. No matter what programming language will have this problem, so there will be a deep copy of this concept.
Because the project is anxious, to use angular.js, modify always modify error, are almost crazy, modified method has not written, just moved the data, the properties of the data changed. Alas. Come on.
The difference between copy assignment and = assignment in Angular.js