AngularJSAngularJS API之extend扩张对象

angular.extend(dst,src),在自己尝试的1.2.16版本上是支撑深拷贝的。然而最新的API展现,这些办法是不协助深拷贝的。
另外,第二个参数src补助三个目的。

第一种采纳方法

var user3 = angular.extend(user1,user2);

会把user2的有所属性都拷贝到user1上,并且把user1复制给user3

AngularJS,第二种选用办法

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

相关文章