<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>angular.extend</title> </head> <body ng-app="extendExample"> <h1>angular.extend</h1> <p> <span>描述:</span><br /> <span>复制src对象中的属性去dst对象中. 支持多个src对象. 如果你不想改变一个对象,你可以把dst设为空对象{}: var object = angular.extend({}, object1, object2). 注意: angular.extend不支持递归复制.</span> </p> <p> <span>使用方法:</span><br /> <span>angular.extend(dst, src)</span> </p> <p> <span>参数:</span><br /> <span>dst:Object,目标对象</span><br /> <span>scr:Object,被copy的对象</span> </p> <p> <span>返回值:</span><br /> <span>对dst的引用</span> </p> <hr /> <div ng-controller="extendcon"> </div> </body> <script src="../../js/angular.1.3.0.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> angular.module('extendExample', []).controller('extendcon', function($scope) { $scope.baby = { cry: function() { console.log('小孩子会哭!!!'); } }; $scope.adult = { earn: function() { console.log('成年人会赚钱!!!'); }, lover: { love: function() { console.log('成年人会有爱!!!'); } } }; $scope.human = {}; $scope.hehe = 'hehe'; $scope.extend = function() { angular.extend($scope.human, $scope.baby, $scope.adult); $scope.human.cry(); $scope.human.earn(); //注意,这里修改了lover对象的love()方法,由于extends()方法不是深复制的,$scope.human和$scope.adult其实引用的是同一个对象 $scope.human.lover.love = function() { console.log("成年人会讨厌人"); } //这两行都会输出“成年人会讨厌人",可怜的adult对象,他把自己的lover分享给了human! $scope.human.lover.love(); $scope.adult.lover.love(); } $scope.extend(); }) </script> </html>