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. CUDA JPEG编码

    基于英伟达的jpegNPP工程,分离实现独立的JPEG压缩. 由于原工程是直接把解码时的jpeg图片的信息直接作为编码时的信息,所以在做独立的JPEG编码时,需要自己来填充各种信息. 1.JPEG编码 ...

  2. IOS-第三方开源库

    我找的琐碎框架 1.HZExtend:应用快速开发架构集合了MVC和MVVM的特点https://github.com/GeniusBrother/HZExtend : 1.减少控制器压力以及耦合 2 ...

  3. Highcharts 测量图;Highcharts 圆形进度条式测量图;Highcharts 时钟;Highcharts 双轴车速表;Highcharts 音量表(VU Meter)

    Highcharts 测量图 配置 chart.type 配置 配置 chart 的 type 为 'gauge' .chart.type 描述了图表类型.默认值为 "line". ...

  4. leetcode 559. Maximum Depth of N-ary Tree

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  5. http请求的GET和POST请求:查询和新增(ajax)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  6. jquery表单验证插件 jquery.form.js-转

    来自:http://www.cnblogs.com/luluping/archive/2009/04/15/1436177.html Form插件,支持Ajax,支持Ajax文件上传,功能强大,基本满 ...

  7. jenkins执行xctool命令出现command not found问题解决方法

    1.控制台执行 echo $PATH 把输出的这句话复制 2.jenkins->系统管理->系统设置 勾选Environment variables,添加键值,键:PATH,值:刚才复制的 ...

  8. 如何写一个LaTeX类文件,并设计你自己的简历

    2017/8/29 20:26:03 原文地址 https://www.sharelatex.com/blog/2011/03/27/how-to-write-a-latex-class-file-a ...

  9. 自回归模型(AR )

    2017/7/2 19:24:15 自回归模型(Autoregressive Model,简称 AR 模型)是最常见的平稳时间序列模型之一.接下将介绍 AR 模型的定义.统计性质.建模过程.预测及应用 ...

  10. 【测试技术】websocket-client

    Websocket Client 继续上周的议题.因为我不会写go,不会写websocket客户端,导致整个测试过程我是个完美的酱油党.上周我终于把客户端服务写好了. 选择Websokcet框架 现在 ...