关于angular.extend的用法

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

关于angular.extend(dst, src);经过从src对象复制全部属性到dst来扩展目标对象dst。你能够指定多个src对象。html

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

 

经典的demo ;angularjs

<!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>app

结果:spa

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

 

 

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

相关文章
相关标签/搜索