Angularjs - 在module.config中动态添加值

时间:2014-09-13 14:57:01

标签: javascript angularjs

var app = angular.module("myApp",['ui.router','flow']);
    app.config(['flowFactoryProvider', function (flowFactoryProvider) {
        flowFactoryProvider.defaults = {
        target: 'upload.php?type=1',
        testChunks:false,
        singleFile: true,
        permanentErrors: [404, 500, 501],
        maxChunkRetries: 1,
        chunkRetryInterval: 5000,
        simultaneousUploads: 4
    };  
}]);

以上代码工作正常......

我只想从$ scope variable

动态更改目标

我认为它应该像

var app = angular.module("myApp",['ui.router','flow']);
    app.config(['flowFactoryProvider', function (flowFactoryProvider) {
        flowFactoryProvider.defaults = {
        target: 'upload.php?type=' + $scope.vtype,
        testChunks:false,
        singleFile: true,
        permanentErrors: [404, 500, 501],
        maxChunkRetries: 1,
        chunkRetryInterval: 5000,
        simultaneousUploads: 4
    };  
}]);

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我能想到的两种可能方式:

导入$ rootScope并将其设置在那里:

var app = angular.module("myApp",['ui.router','flow']);
    app.config(['flowFactoryProvider', function (flowFactoryProvider, $rootScope) {
        flowFactoryProvider.defaults = {
            target: 'upload.php?type=' + $rootScope.vtype,
            testChunks:false,
            singleFile: true,
            permanentErrors: [404, 500, 501],
            maxChunkRetries: 1,
            chunkRetryInterval: 5000,
            simultaneousUploads: 4
        };  
    }
]);

在某些控制器中,您可以将其设置为

$rootScope.vtype = something;

或者您可以在提供程序中编写一个get / set方法,以允许您更改本地值。

var app = angular.module("myApp",['ui.router','flow']);
    app.config(['flowFactoryProvider', function (flowFactoryProvider) {

        var someLocalValue = 1; // default value

        flowFactoryProvider.defaults = {
            target: 'upload.php?type=' + someLocalValue,
            testChunks:false,
            singleFile: true,
            permanentErrors: [404, 500, 501],
            maxChunkRetries: 1,
            chunkRetryInterval: 5000,
            simultaneousUploads: 4
        };  
        flowFactoryProvider.getSomeLocalValue = function(){
            return someLocalValue;
        };
        flowFactoryProvider.setSomeLocalValue = function(input){
            flowFactoryProvider.defaults.target = 'upload.php?type=' + input;
            someLocalValue = input;
        };
    }
]);