关于angular.extend的用法
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的用法的更多相关文章
- angular.extend用法实例
angular.extend:依次将第二个参数及后续的参数的第一层属性(不管是简单属性还是对象)拷贝赋给第一个参数的第一层属性,即如果是对象,则是引用的是同一个对象,并返回第一个参数对象. 实例一 ...
- angular.extend()和 angular.copy()的区别
1.angular.copy angular.copy(source, [destination]); // source: copy的对象. 可以使任意类型, 包括null和undefined. ...
- AngularJs angular.forEach、angular.extend
angular.forEach 调用迭代器函数取每一项目标的集合,它可以是一个对象或数组.迭代器函数与迭代器(value.key)一起调用,其中值是一个对象属性或数组元素的值,而数组元素是对象属性的关 ...
- angular.js 的angular.copy 、 angular.extend 、 angular.merge
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- angular.extend(dst, src)对象拓展
angular.extend(dst, src) 作用:对象的拓展 参数: dst:拓展的对象 src:源对象 返回值:拓展的对象 var dst = {name: 'xxx', country: ...
- angular.extend
function f1() {} var f2 = angular.extend(f1, { active: false, toggle: function() { this.active = !th ...
- Angular - - angular.forEach、angular.extend
angular.forEach 调用迭代器函数取每一项目标的集合,它可以是一个对象或数组.迭代器函数与迭代器(value.key)一起调用,其中值是一个对象属性或数组元素的值,而数组元素是对象属性的关 ...
- angular.extend、angular.$watch、angular.bootstrap
1.angular.extend:依次将第二个参数及后续的参数的第一层属性(不管是简单属性还是对象)拷贝给第一个参数的第一层属性,即如果是对象,则是引用的是同一个对象,并返回第一个参数对象. 直接上代 ...
- angular.extend深拷贝(deep copy)
在用到angular.extend的时候,正好碰到一个对象,是层层嵌套的Array, 结果发现只能extend第一层,查阅官文档,确实不支持deep copy: Note: Keep in mind ...
随机推荐
- mysql数据库基础知识和认识
mysql 创建一个用户 hail,密码 hail,指定一个数据库 haildb 给 hail mysql -u root -ppassworduse mysql;insert into user(h ...
- LeetCode OJ:Reverse Nodes in k-Group(K个K个的分割节点)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 转载-lvs官方文档04-LVS集群的负载调度
LVS集群的负载调度 章文嵩 (wensong@linux-vs.org) 2002 年 5 月 本文主要讲述了LVS集群的IP负载均衡软件IPVS在内核中实现的各种连接调度算法.针对请求的服务时间变 ...
- 随手写的一个检测php连接mysql的小脚本
最近偶然接触到一点点的php开发,要用到mysql数据库,由于mysql和php版本的关系,php5里面连接函数有mysql_connect(),mysqli_connect()两种,php7中又使用 ...
- LVS模式三:NAT模式
一.NAT模式 NAT(Network Address Translation,网络地址转换).数据包传输过程就是把客户端发来的数据包的IP头的目的地址,在负载均衡器上换成其中一台RS的IP地 ...
- InfluxDB使用纪录
我是Mac环境 1.安装 brew install influxdb 安装完成后,默认目录为/usr/local/opt/influxdb. 2.安装完成后,打开influxdb服务. $ influ ...
- c# http操作类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- Java8安装配置
方法1.命令行安装 -jdk 存在多个版本,自动配置: sudo update-alternatives --config java 方法2.下载安装 下载java8的jdk http://www.o ...
- php开发中一些前端知识杂总
推荐几个jqyuey插件的好地方 http://jqueryui.com/ http://www.jq22.com/ 背景: 服务端采用ci3.0框架,twig作为模板嵌套. twig模板手册: ht ...
- 关于python机器学习常用算法的例子
Home Installation Documentation Examples Previous An introduction ... This documentation is for ...