gpt4 book ai didi

angularjs - 有条件地注入(inject) Angular 模块依赖

转载 作者:行者123 更新时间:2023-12-03 06:54:16 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com