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. Uva11464 Even Parity

    枚举每个格子的状态显然是不可能的. 思考发现,矩阵第一行的状态确定以后,下面的状态都可以递推出来. 于是状压枚举第一行的状态,递推全图的状态并判定是否可行. /*by SilverN*/ #inclu ...

  2. PHP设计模式(二)

    从最近开始我给自己定了个目标,每周至少更新2篇博客,用来记录自己在上一周里面遇到的问题或者想出的新点子,一方面对自己掌握的知识进行记录,免得时间久了忘得一干二净,二来我的博文虽然不怎么好但也许会对一小 ...

  3. Code笔记 之:防盗链(图片)

    图片防盗链   参考:http://bbs.csdn.net/topics/330080045    应该是”10种图片防盗的方法“,而不是”10种图片防盗链的方法“,不过看搜索防盗链的人要多一点,所 ...

  4. 获取URL列表,设置代理请求URL,https的加密方式处理

    做了一个测试的一个小工具,需求如下: 1.有一批URL列表,需要知道哪个URL请求响应内容中包含http:关键字的. 2.url请求包括http和https 2种协议 3.要部署在linux服务器上, ...

  5. 捉襟见肘之UITableViewCell重用引发的问题

    我记录一下自己如何解决cell内容重叠的问题 首先,复习一下:http://blog.csdn.net/omegayy/article/details/7356823 UITableViewCell的 ...

  6. BZOJ1180: [CROATIAN2009]OTOCI

    传送门 一遍AC,开心! $Link-Cut-Tree$最后一题 //BZOJ 1180 //by Cydiater //2016.9.18 #include <iostream> #in ...

  7. 时间日期----java

    Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...

  8. Java数据库——PreparedStatement接口

    PreparedStatement接口是Statement的子接口,属于预处理操作,与直接使用Statement不同的是,PreparedStatement在操作时,是先在数据表中准备好了一条SQL语 ...

  9. BigDecimal类

    如果需要精确的计算结果,则必须使用BigDecimal类,而且使用BigDecimal类也可以进行大数的操作. //========================================== ...

  10. rem自适应布局的回顾总结

    使用rem实现自适应布局,应该算是当前移动前端的一大趋势,有些人对此还有点迷惑,搞不懂rem是如何实现自适应布局,如何根据设计稿来调整rem的值?rem布局如何用雪碧背景图片?rem一定要加载JS吗? ...