AngularJS API之extend扩展对象

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

相关文章