有条件地注入角度模块依赖性 [英] Conditionally inject angular module dependency

查看:71
本文介绍了有条件地注入角度模块依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angle的新手,有以下代码.

angular.module('MyApp')
    .controller('loginController', ['$scope', '$http', 'conditionalDependency',
        function ($scope, $http, conditionalDependency{
}

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

if(true)
{
//add conditionalDependency
}

这怎么做.我看过这篇文章.但是,我的要求是我必须在 function

中指定依赖项

谢谢.

解决方案

不清楚为什么您会必须将其包含在示例函数中的命名函数中,但是...

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

在AngularJS中有条件地注入服务

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

示例:

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

        if (something) {
            service = $injector.get('myService');
        }
    });
 

I'm new to angular and have the following code.

angular.module('MyApp')
    .controller('loginController', ['$scope', '$http', 'conditionalDependency',
        function ($scope, $http, conditionalDependency{
}

I would like to have conditionalDependency loaded conditionally. Something like this

if(true)
{
//add conditionalDependency
}

How can this be done. I've seen this post . However, my requirement is that I have the dependency specified in function

Thanks in advance.

解决方案

Not quite clear as to why you would have to have it in a named function like in your example but...

If you need conditional dependencies, I would suggest taking a look at the following:

Conditional injection of a service in AngularJS

I've used this method in a couple niche scenarios and it works quite well.

EXAMPLE:

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

        if (something) {
            service = $injector.get('myService');
        }
    });

这篇关于有条件地注入角度模块依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆