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

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>

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.


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.