AngularJS指令默认选项克隆

时间:2014-05-20 11:52:53

标签: angularjs

我有一个我制作的angularjs指令。它有一个默认对象,如下所示:

module.constant('tableDefaults', {
    hasHeader: true,
    paging: {
         autoPageSize: true,
         count: undefined,
         pageSize: undefined
    }
});

该指令使用该常量并合并传入的选项,如:

module.directive('ngTable', function ($rootScope, tableDefaults) {
    return {
        restrict: 'AE',
        transclude: true,
        replace: true,
        require: '^ngModel',
        templateUrl: 'common/components/table/views/table.tpl.html',
        controller: function(){
            // freaks out if removed ...
        },
        link: function ($scope, $element, $attributes, ctrls) {
            if(!$scope.tableOptions) $scope.tableOptions = {}
            $scope.tableOptions = angular.copy(tableDefaults, $scope.tableOptions);
        } 
    };
});

我传递的选项如下:

<div ng-table="{ paging: { count: 100 } }" />

问题现在在我的分页对象中,没有其他选项存在。我一直在关注angular.extend vs $.extend,并意识到我可能会使用$.extend来完成此任务,但我正试图摆脱我的应用程序jquery。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我尝试在此处重现您的问题,但我看到tableOptions已正确设置为tableDefaults。我不知道transclude创建的额外范围是否可能成为问题(我在演示中将其删除),但我不知道如何。查看console.debugFiddle

相关问题