AngularJS API之extend扩展对象


AngularJS API之extend扩展对象 - xingoo - 博客园 var currentBlogApp = 'xing901022', cb_enable_mathjax=true;var isLogined=false;
load_page_begin_html();

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>-
posted @ 2015-11-04 22:23 xingoo 阅读(...) 评论(...) 编辑 收藏
markdown_highlight();var allowComments=true,cb_blogId=128528,cb_entryId=4937651,cb_blogApp=currentBlogApp,cb_blogUserGuid='08d856da-3501-e211-aa3f-842b2b196315',cb_entryCreatedDate='2015/11/4 22:23:00';loadViewCount(cb_entryId);
var commentManager = new blogCommentManager();commentManager.renderComments(0);
刷新评论刷新页面返回顶部
$(function () { setTimeout(function () { incrementViewCount(cb_entryId); }, 50); deliverAdT2(); deliverAdC1(); deliverAdC2(); loadNewsAndKb(); loadBlogSignature(); LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid); GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate); loadOptUnderPost(); GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate); });

最新文章