angular提供了一个可以复制对象的api——copy(source,destination),它会对source对象执行深拷贝。

使用时需要注意下面几点:

  • 如果只有一个参数(没有指定拷贝的对象),则返回一个拷贝对象
  • 如果指定了destination,则会深拷贝对象复制给destination
  • 如果source是null或者undefined,那么会直接返回source
  • 如果source就是desitination,那么会报错。

下面看看使用样例:

<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>form = {{user | json}}</pre>
<pre>master = {{master | json}}</pre>
</div> <script>
angular.module('copyExample', [])
.controller('ExampleController', ['$scope', 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>
</body>
</html>

AngularJS API之copy深拷贝的更多相关文章

  1. JS Object Deep Copy & 深拷贝 & 浅拷贝

    JS Object Deep Copy & 深拷贝 & 浅拷贝 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Refe ...

  2. 搭建angularjs API文档站点

    提供一个国内可以访问的 angularjs API文档站点 http://i.frllk.com/ 文档直接在 github 上下载的: https://github.com/angular-cn/n ...

  3. 【16】AngularJS API

    AngularJS API API 意为 Application Programming Interface(应用程序编程接口). AngularJS 全局 API AngularJS 全局 API ...

  4. JS Object Deep Copy & 深拷贝

    JS Object Deep Copy & 深拷贝 针对深度拷贝,需要使用其他方法 JSON.parse(JSON.stringify(obj));,因为 Object.assign() 拷贝 ...

  5. AngularJS API

    AngularJS 全局 API 用于执行常见任务的 JavaScript 函数集合 angular.lowercase() 转换字符串为小写 angular.uppercase() 转换字符串为大写 ...

  6. AngularJS API之$injector ---- 依赖注入

    在AngularJS中也有依赖注入的概念,像spring中的依赖注入,但是又有所不同.Spring中使用构造注入或者设值注入的方式,还需要做一些额外的操作,但是angular中只需要在需要的地方声明一 ...

  7. AngularJS API之bootstrap启动

    对于一般的使用者来说,AngularJS的ng-app都是手动绑定到某个dom元素.但是在一些应用中,这样就显得很不方便了. 绑定初始化 通过绑定来进行angular的初始化,会把js代码侵入到htm ...

  8. AngularJS API之extend扩展对象

    angular.extend(dst,src),在我实验的1.2.16版本上是支持深拷贝的.但是最新的API显示,这个方法是不支持深拷贝的. 另外,第二个参数src支持多个对象. 第一种使用方式 va ...

  9. [CareerCup] 13.4 Depp Copy and Shallow Copy 深拷贝和浅拷贝

    13.4 What is the difference between deep copy and shallow copy? Explain how you would use each. 这道题问 ...

随机推荐

  1. Android成长日记-使用PagerAdapter实现页面切换

    Tip:此方式可以实现页面切换 1. 创建view1.xml,view2.xml,view3.xml,main.xml 在main.xml中创建 <android.support.v4.view ...

  2. A.Kaw矩阵代数初步学习笔记 6. Gaussian Elimination

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  3. DS18B20函数库建立实验

    1.主代码: /* 温度传感器  */#include "DS18B20.h"#include"def.h"u16 get_temp (void){    fl ...

  4. SQL Server编程(04)基本语法【转载】

    一.定义变量 --简单赋值 declare @a int set @a=5 print @a   --使用select语句赋值 declare @user1 nvarchar(50) select @ ...

  5. spring-boot-note

    1 java配置和注解配置相结合,不需要任何的xml配置即可 2 spring tool suite 3 src/main/resources/banner.txt http://patorjk.co ...

  6. ubuntu下vim输入中文和中文显示

    安装和配置VIM,参考   http://jingyan.baidu.com/album/046a7b3efd165bf9c27fa915.html?picindex=4 在home/你的用户名 这个 ...

  7. JavaScript学习笔记——基本知识

    JavaScript学习的教程来自后盾网 1>JavaScript的放置和注释 1.输出工具 A.alert(); B.document.write(); C.prompt("&quo ...

  8. C++ Pointer-to-Member Selector

    http://www.codeguru.com/cpp/cpp/article.php/c17401/C-Tutorial-PointertoMember-Function.htm https://m ...

  9. Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast

    Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast的解决方法: 在命令行输入:yum clean al ...

  10. SCWS分词扩展在UNIX/LINUX下的安装方法

    <?php/** * 中文分词处理方法 *+--------------------------------- * @param stirng  $string 要处理的字符串 * @param ...