关于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 ...
随机推荐
- Prism 4 文档 ---第10章 Silverlight和WPF之间共享代码
本主题帮助你理解来自Prism的多目标和它的优缺点.多目标的代码针对两个不同的平台有大致相同的代码库.这允许同时保持代码尽可能多一样能够产生针对两种不同技术的二进制文件.在这种情况下,本节介绍 ...
- LeetCode OJ:Jump Game II(跳跃游戏2)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- web服务器无法显示font-awesome字体图标
今天遇到了在本地运行网页 一切调用的额font的小图标都OK的,但是把网页发布到tomcat服务器上面就不行了 之后百度了下,找到了解决方法,遂记录下,方法如下: 在web.xml 文件中加上: &l ...
- awk与sed:一个关于多行处理的例子
前几天在CSDN上看到一个帖子http://bbs.csdn.net/topics/390848841,楼主贴了下面的问题: 例: 12345 67890 1234567890 123 4567890 ...
- shell脚本实例二
练习题一:对已经存在的用户做密码的修改等操作 vim user_ctrl.sh ##进行如下编写 #!/bin/bashShow(){ read -p ...
- c# 处理js序列化时 datetime返回UTC格式的问题
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...
- PHP sessions that have already been started
In my PHP code, if a session has already started, and I try to start a new one, I get the following ...
- DOM 踩踩踩
1.如果是想给一个DOM元素添加一个伪类,可以转换为 为这个元素添加一个类名,这个类名上面绑定一个伪类. 2.append一个元素,删除掉原来的元素再进行添加.
- Kotlin Reference (三) Coding Conventions
most from reference 命名规则 1.使用驼峰式命名规则,尽量避免在命名中使用下划线 2.类型以大写字母开头 3.方法和属性以小写字母开头 4.使用4个空格缩进 5.public的方法 ...
- NamedParameterJdbcTemplate常用方法总结
数据库结构 1.插入/修改/删除数据,使用update方法 插入数据1 API: int update(String sql, Map< String, ? > paramMap) 示例: ...