The difference between copy assignment and = assignment in Angular.js

Source: Internet
Author: User

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:

    1. There's an address in memory that stores { firstName: "John", lastName: "Doe" } this data, and it starts with $scope.master pointing to it.

    2. 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.

    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.