Home >Web Front-end >JS Tutorial >AngularJS API copy deep copy detailed explanation and examples

AngularJS API copy deep copy detailed explanation and examples

高洛峰
高洛峰Original
2017-01-03 15:54:161676browse

Angular provides an API that can copy objects - copy(source,destination), which will perform a deep copy of the source object.

You need to pay attention to the following points when using it:

If there is only one parameter (no copied object is specified), a copy object will be returned

If destination is specified , the object will be deeply copied to destination

If source is null or undefined, source

will be returned directly. If source is destination, an error will be reported.

Let’s take a look at the usage example:

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
</head>
<body ng-app="copyExample">
  <div ng-controller="ExampleController">
    <form novalidate class="simple-form">
      Name: <input type="text" ng-model="user.name" /><br />
      E-mail: <input type="email" ng-model="user.email" /><br />
      Gender: 
      <input type="radio" ng-model="user.gender" value="male" />
      male
      <input type="radio" ng-model="user.gender" value="female" />
      female
      <br />
      <button ng-click="reset()">RESET</button>
      <button ng-click="update(user)">SAVE</button>
    </form>
    <pre class="brush:php;toolbar:false">form = {{user | json}}
master = {{master | json}}
<script> angular.module(&#39;copyExample&#39;, []) .controller(&#39;ExampleController&#39;, [&#39;$scope&#39;, function($scope) { $scope.master= {}; var test1; console.log(angular.copy(test1));//undefined var test3=null; console.log(angular.copy(test2));//undefined var test2 = "a"; // console.log(angular.copy(test2,test2));//error!! $scope.update = function(user) { // Example with 1 argument $scope.master= angular.copy(user); }; $scope.reset = function() { // Example with 2 arguments angular.copy($scope.master, $scope.user); console.log($scope.master); console.log($scope.user); }; $scope.reset(); }]); </script>

AngularJS API之copy深拷贝详解及实例

The above is the information about copy deep copy of AngularJS API, and we will continue to add relevant information in the future. , thank you all for your support of this site!

For more detailed explanations and examples of copy deep copy of AngularJS API, please pay attention to the PHP Chinese website!

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
Previous article:Detailed explanation of shallow copy and deep copy in javascriptNext article:Detailed explanation of shallow copy and deep copy in javascript

Related articles

See more