ng中的ng-function中会有些方法,便于我们进行js代码的编写

关于angular.extend(dst, src);通过从src对象复制所有属性到dst来扩展目标对象dst。你可以指定多个src对象。

注意:angular.extend(....)只是简单的对象之间的相互引用,

经典的demo ;

<!DOCTYPE html>
<html ng-app="extendApp">
<head>
<meta charset="UTF-8">
<title></title>
<script src="http://code.angularjs.org/1.2.3/angular.min.js"></script>
</head>
<body>

<div ng-controller="extendController">

<button ng-click="extend()">点击我!</button>

</div>
</body>
</html>

<script type="text/javascript">
angular.module("extendApp", [])
.controller("extendController", function($scope)
{
$scope.baby =
{
cry : function()
{
console.log("I can only cry!");
}
}

$scope.adult =
{
earn : function()
{
console.log("I can earn money!");
},
lover:
{
love:function()
{
console.log("I love you!");
}
}
}
$scope.human = {}

$scope.hehe = "hehe ";

$scope.extend = function()
{
angular.extend($scope.human, $scope.baby, $scope.adult);
$scope.human.cry();
$scope.human.earn();
// $scope.human 和$scope.adult其实引用的是同一个对象-
$scope.human.lover.love = function()
{
console.log("I hate you!");
}

//这两行都会输出“I hate you !",可怜的adult对象, 他把自己的lover分享给了human! -->
$scope.human.lover.love();
$scope.adult.lover.love();
}
});
</script>

结果:

I can only cry!
  I can earn money!
  I hate you!
  I hate you!

拓展:关于对象,数组的复制,我们可以进一步的了解angular.copy();的使用方法

关于angular.extend的用法的更多相关文章

  1. angular.extend用法实例

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

  2. angular.extend()和 angular.copy()的区别

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

  3. AngularJs angular.forEach、angular.extend

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

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

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

  5. angular.extend(dst, src)对象拓展

    angular.extend(dst, src) 作用:对象的拓展 参数:  dst:拓展的对象 src:源对象 返回值:拓展的对象 var dst = {name: 'xxx', country: ...

  6. angular.extend

    function f1() {} var f2 = angular.extend(f1, { active: false, toggle: function() { this.active = !th ...

  7. Angular - - angular.forEach、angular.extend

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

  8. angular.extend、angular.$watch、angular.bootstrap

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

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

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

随机推荐

  1. OGNL表达式语言中的"#"和"$"的区别

    Struts2使用标准的Context来进行OGNL表达式语言求值,OGNL的顶级对象是一个Context,这个Context对象就是一个Map类型实例,其根对象就是ValueStack,如果需要访问 ...

  2. I/O流+统计文件词频

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  3. LeetCode OJ : Different Ways to Add Parentheses(在不同位置增加括号的方法)

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  4. js面向对象之:创建对象

    最近在学习<js高级程序设计>,之前所接触的很多的js类库和jQuery插件都会用面向对象的方式来设计,而自己却还是停留在面向方法的阶段,所以今天好好记录一下学习的js创建对象. 第一种方 ...

  5. 初次使用VCS仿真软件

    由于刚开始接触VCS,对于VCS不是太了解,在网上找了很多的资料终于遇到了一个相对比较初级的入门资料,这个资料是以一个简单的4位加法器为例来介绍vcs的用法的,比较好入门,这个文章的地址如下: htt ...

  6. shell脚本实例三

    练习一:获得连通主机的ip和hostname1.脚本编写 vim  checkhost.sh #!/bin/bashAuto_conn(){/usr/bin/expect << EOFse ...

  7. IOS开发 基础框架(Fondation Framework)的线程安全

    有一种误解,认为基础框架(Foundation framework)是线程安全的,而Application Kit是非线程安全的.不幸的是,这是一个总的概括,从而造成一点误导.每个框架都包含了线程安全 ...

  8. artDialog的使用

    用法一:github上下载包:https://github.com/aui/artDialog                seajs方法使用 <!doctype html> <h ...

  9. iOS-----GitHub上比较齐全的iOS 工具和App

    Github-iOS 工具 和 App   系统基础库 Category/Util sstoolkit 一套Category类型的库,附带很多自定义控件 功能不错-       BFKit 又一套Ca ...

  10. xshell配置密码公钥登录

    1:生成公钥 2:密钥类型选择为rsa,长度为2048,并点击下一步,如下所示: 这里的密码是给密钥设置了密码,那么在使用这个密钥时也要输入密码,也可以设置为空 3:保存密钥 (1):当你选择了输入密 ...