有条件地注入角度模块依赖性

Fre*_*enz 8 angularjs

我是角度新手并拥有以下代码.

angular.module('MyApp')
    .controller('loginController', ['$scope', '$http', 'conditionalDependency',
        function ($scope, $http, conditionalDependency{
}
Run Code Online (Sandbox Code Playgroud)

我想有条件地加载conditionalDependency.像这样的东西

if(true)
{
//add conditionalDependency
}
Run Code Online (Sandbox Code Playgroud)

如何才能做到这一点.我看过这篇文章 .但是,我的要求是我在函数中指定了依赖项

提前致谢.

tec*_*osh 15

不太清楚为什么你必须在你的例子中的命名函数中,但...

如果您需要条件依赖项,我建议您查看以下内容:

在AngularJS中有条件地注入服务

我已经在几个小众场景中使用了这种方法,并且效果很好.

例:

angular.module('myApp').controller('loginController', 
    ['$injector', '$scope', '$http',
    function($injector, $scope, $http) {
        var service;

        if (something) {
            service = $injector.get('myService');
        }
    });
Run Code Online (Sandbox Code Playgroud)