我是 Angular 新手,有以下代码。
angular.module('MyApp')
.controller('loginController', ['$scope', '$http', 'conditionalDependency',
function ($scope, $http, conditionalDependency{
}
我希望有条件地加载conditionalDependency。像这样的事情
if(true)
{
//add conditionalDependency
}
这怎么能做到。我见过this post 。但是,我的要求是我具有 function
中指定的依赖项提前致谢。
最佳答案
不太清楚为什么您必须将其放在像示例中这样的命名函数中,但是......
如果您需要条件依赖项,我建议您查看以下内容:
Conditional injection of a service in AngularJS
我已经在几个小众场景中使用了这种方法,并且效果非常好。
示例:
angular.module('myApp').controller('loginController',
['$injector', '$scope', '$http',
function($injector, $scope, $http) {
var service;
if (something) {
service = $injector.get('myService');
}
});
关于angularjs - 有条件地注入(inject) Angular 模块依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29617903/