1.angular.copy

angular.copy(source, [destination]);  // source: copy的对象. 可以使任意类型, 包括null和undefined.    destination:接收的对象   返回复制或更新后的对象

如果省略了destination,一个新的对象或数组将会被创建出来;
如果提供了destination,则source对象中的所有元素和属性都会被复制到destination中;
如果source不是对象或数组(例如是null或undefined), 则返回source;
如果source和destination类型不一致,则会抛出异常。
 
angular.copy后,改变source不会影响destination,改变destination 不会改变source
var source=[1,'source',{id:1,source:'hello'}];
var des=angular.copy(source);
source[0]=9;
source[2].source='change';
console.log(des); //输出des不会变化
var source=[1,'source',{id:1,source:'hello'}];
var des=angular.copy(source);
des[0]=9;
des[2].source="change";
console.log(source); //输出source不变

2、angular.extend

angular.extend(destination, source);

复制src对象中的属性去dst对象中. 支持多个src对象. 如果你不想改变一个对象,你可以把dst设为空对象{}: var object = angular.extend({}, object1, object2). 注意: angular.extend不支持递归复制.

如果复制的是个基本数值,则改变source不会影响des,改变des不会影响source

var source=[1,'source',{id:1,source:'hellow'}];
var des=angular.extend(source);
source[0]=9;
source[2].source='change';
console.log(des);//des改变为和source一致
var source=[1,'source',{id:1,source:'hello'}];
var des=angular.extend(source);
des[0]=9;
des[2].source="change";
console.log(source); // source改变

多个对象复制时

 var modalOptions = {
closeButtonText: '取消',
actionButtonText: '确定',
headerText: 'Proceed?',
bodyText: 'Perform this action?',
backdrop:'false',
type:'1',
confirm:false
};
var customModalDefaults={
backdrop: 'static'
};
var temp={};
console.log(angular.extend(temp,modalOptions,customModalDefaults));
console.log(temp.backdrop);// static 我猜想可能是后面的对象的相同属性先替换前一个对象的相同属性,然后层层迭代,再传给目标对象 需要注意的是,虽是层层迭代,但modalOptions不会变,他的backdrop=false
console.log(angular.extend(temp,customModalDefaults,modalOptions));
console.log(temp.backdrop); // false 当多个源对象的属性不冲突时,就会合并一起传给目标对象

angular.extend()和 angular.copy()的区别的更多相关文章

  1. angular.js 的angular.copy 、 angular.extend 、 angular.merge

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. angular.extend深拷贝(deep copy)

    在用到angular.extend的时候,正好碰到一个对象,是层层嵌套的Array, 结果发现只能extend第一层,查阅官文档,确实不支持deep copy: Note: Keep in mind ...

  3. angular.extend vs angular.copy

    1.angular.copy angular.copy(source, [destination]);  // source: copy的对象. 可以使任意类型, 包括null和undefined. ...

  4. angular.extend、angular.$watch、angular.bootstrap

    1.angular.extend:依次将第二个参数及后续的参数的第一层属性(不管是简单属性还是对象)拷贝给第一个参数的第一层属性,即如果是对象,则是引用的是同一个对象,并返回第一个参数对象. 直接上代 ...

  5. AngularJs angular.forEach、angular.extend

    angular.forEach 调用迭代器函数取每一项目标的集合,它可以是一个对象或数组.迭代器函数与迭代器(value.key)一起调用,其中值是一个对象属性或数组元素的值,而数组元素是对象属性的关 ...

  6. Angular - - angular.forEach、angular.extend

    angular.forEach 调用迭代器函数取每一项目标的集合,它可以是一个对象或数组.迭代器函数与迭代器(value.key)一起调用,其中值是一个对象属性或数组元素的值,而数组元素是对象属性的关 ...

  7. 关于angular.extend的用法

    ng中的ng-function中会有些方法,便于我们进行js代码的编写 关于angular.extend(dst, src);通过从src对象复制所有属性到dst来扩展目标对象dst.你可以指定多个s ...

  8. angular.extend(dst,src)的简单示例

    自我认为这个方法跟angular.copy(src,dst)有点相似.在angular.extend({},src)时,就可以画等号.这个src只代表一个对象.代码如下:(注意这个src可以有多个对象 ...

  9. angular.extend用法实例

      angular.extend:依次将第二个参数及后续的参数的第一层属性(不管是简单属性还是对象)拷贝赋给第一个参数的第一层属性,即如果是对象,则是引用的是同一个对象,并返回第一个参数对象. 实例一 ...

随机推荐

  1. vmstat和iostat

    一.vmstat1.命令示例#vmstat 5每5秒输出一次 2.输出详解procs -----------memory---------- ---swap-- -----io---- --syste ...

  2. Source Insight 3.X 插件新loader发布

    [前言] 大约一年多没更新SI插件了,主要原因是我从SI换到了sublime,使用SI少了,插件也就停止更新了.不过看到园子里这么多网友的留言,觉得还是有必要更新一下,算是给还在用SI的朋友们一个交代 ...

  3. ORA-29275: partial multibyte character

    查询表报错 修改方式1 和字符集存储方式有关系 ,修改客户端和服务器的字符集存储方害死 修改方式2 修改表的字段由nvarchar2修改为varchar2

  4. WinServer2003 文件夹共享 方法设置

    文件夹设置: 5.假设我们在E盘有一个wmpub文件夹要设置在网络中共享,让大家都可以看到,首先我们右键点击WMPUB文件夹[共享和安全],弹出对话框,选择[共享此文件夹],点击[权限],弹出的对话框 ...

  5. ACM集训的Training Day 3的A题。。。

    A. 等差数列 一.题目描述: 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来 ...

  6. OC中的字典

    // ********************不可变最字典***************** /* NSDictionary * dic = [NSDictionary dictionaryWithO ...

  7. CS193P - 2016年秋 第三讲 Swift 语言及 Foundation 框架

    这一讲介绍一些 Swift 的重点概念.特别是一些有别于其它语言的地方.但本质上还都是语法糖. 想充分理解这一讲的内容,最好的方式就是 打开 playgound,亲自动手来实验. 1,Optional ...

  8. Foreach遍历

    前天在项目中遇到一个问题,foreach遍历过程中修改responses中的对象,其中responses的类型:IEnumerable<Order>,代码如下: foreach (Orde ...

  9. 使用 apache2 + `mod_proxy_uwsgi` + uwsgi + upstart 部署

    使用 apache2 + mod_proxy_uwsgi + uwsgi + upstart 部署 网上运行 python wsgi 的应用时,大部分的资料都是使用 nginx .uwsgi ,很少资 ...

  10. [SharePoint 2013] Subscribe report within SharePoint mode

    param([string]$path, [string]$fileName, [string]$storage) $description = "Save in $storage as $ ...