AngularJS API之extend扩展对象
Author:[email protected] Date:
angular.extend(dst,src),在我实验的1.2.16版本上是支持深拷贝的。但是最新的API显示,这个方法是不支持深拷贝的。
另外,第二个参数src支持多个对象。
第一种使用方式
var user3 = angular.extend(user1,user2);
会把user2的所有属性都拷贝到user1上,并且把user1复制给user3
第二种使用方式
var user3 = angular.extend({},user1,user2);
这种使用方式,会保留原始对象,把user1与user2进行整合,在复制给user3.
样例代码
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js">script>
head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
{{ user1 }}
{{ user2 }}
{{ user3 }}
{{ user4 }}
{{ user5 }}
div>
<script type="text/javascript">
angular.module("myApp",[])
.controller("myCtrl",function($scope){
var user1 = {"name":"xingoo"};
var user2 = {"age":30};
var user3 = angular.extend(user1,user2);
// var user3 = angular.extend({},user1,user2);
$scope.user1 = user1;
$scope.user2 = user2;
$scope.user3 = user3;
var user4 = new Object();
user4.name = "xingoo";
user4.age = 30;
user4.mother = user1;
var user5 = new Object();
angular.extend(user5,user4);
$scope.user4 = user4;
$scope.user5 = user5;
});
</script>
body>
</html>-
转载本站文章《AngularJS API之extend扩展对象》,
请注明出处:https://www.zhoulujun.cn/html/webfront/ECMAScript/angularjs/2015_1224_375.html