Home > php教程 > PHP开发 > Use angular.copy to cancel the two-way binding and parsing of variables

Use angular.copy to cancel the two-way binding and parsing of variables

高洛峰
Release: 2016-12-05 11:40:03
Original
1537 people have browsed it

First let’s take a look at the sample code

<body ng-app="app">
 <div ng-controller="CopyController">
 <div>
 data: <input ng-model="user.data" /><br>
 user.data: {{user.data}} <br>
 user1.data: {{user1.data}} <br>
 <button ng-click="changeData1()">change</button> <br>
 <button ng-click="copy()">copy</button> <br>
 copyData: {{copyUser.data}}
 </div>
 </div>
 <script src="node_modules/angular/angular.min.js"></script>
</body>
<script>
 angular.module(&#39;app&#39;, [])
 .controller(&#39;CopyController&#39;, function ($scope) {
 // body...
 $scope.changeData1 = function () {
  // body...
  scope.user1=scope.user1=
scope.user;
  $scope.user1.data = &#39;is changed&#39;;
 }
 $scope.copy = function () {
  // body...
  scope.copyUser=angular.copy(scope.copyUser=angular.copy(
scope.user);
 }
 });
</script>
Copy after login

As you can see from the above demonstration, when the copy button is clicked, the value of copyData becomes "this is old data", and the value of user is successfully copied to copyUser.

When the change button is clicked, the values ​​​​of user1 and user become 'is changed', but the value of copyUser does not change. At this time, when the value is changed in the input input box, the values ​​​​of user and user1 will change accordingly, indicating that the two are actually the same variable reference. And copyUser has not changed.

angular.copy can cancel the principle of two-way binding

This is related to the fact that objects in JavaScript are reference types.

Value types in JavaScript

In JavaScript, values ​​are divided into two types: primitive values ​​and reference values.

Primitive values: simple data fields stored in the Stack, that is, their values ​​are stored directly at the locations accessed by variables;

Reference values: stored in the heap, that is, The value stored in a variable is a pointer to the memory location where the object is stored.

Objects in JavaScript are reference values, which means that objects pass values ​​by reference.

So in the above code:

The values ​​of objects $scope.user and $scope.user1 both point to the same reference. For Angular, monitoring variable changes is monitoring the address referenced by its object, so when the reference value of the object changes, all objects pointing to it will change accordingly.

So in Angular, two-way binding cannot be released directly through object assignment. So the way to cancel two-way binding is to create a new object and then assign the value of the original object to the new object. Isn't this just a deep copy in JavaScript?

Yes, angular.copy is the deep copy method provided by Angular. Therefore, the object copied through angular.copy can be consistent with the original object value, and does not point to the same reference as the old object, thus realizing the two-way binding of the object variable.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template