在 config() 模块中注入依赖项 - AngularJS [英] Injecting Dependencies in config() modules - AngularJS
问题描述
目前在 app.js 中我有以下路线:
Currently in app.js i have the following routes:
var gm = angular.module('gm', ['gm.services','gm.directives','gm.filters','gm.controllers','ngSanitize']);
gm.config(['$routeProvider', 'Path', function($routeProvider, Path) {
$routeProvider.when('/login', {
templateUrl: Path.view('application/authentication/login.html'),
controller: 'authController'
});
$routeProvider.when('/dashboard', {
templateUrl: Path.view('application/dashboard/index.html'),
controller: 'dashboardController'
});
$routeProvider.otherwise({
redirectTo: '/login'
});
}]);
如您所见,我正在尝试注入 Path 依赖项.虽然我收到一个错误,说它找不到这个提供者.我认为这是因为配置模块提供程序在执行其他任何操作之前首先执行.下面是我在services.js"中的路径提供者定义
I'm trying to inject the Path dependency as you can see. Although i get an error saying it can't find this provider. I think this is because config module providers are executed first before anything else. below is my Path provider definition in "services.js"
gm.factory("Path", function() {
return {
view: function(path) {
return 'app/views/' + path;
},
css: function(path) {
return 'app/views/' + path;
},
font: function(path) {
return 'app/views/' + path;
},
img: function(path) {
return 'app/views/' + path;
},
js: function(path) {
return 'app/views/' + path;
},
vendor: function(path) {
return 'app/views/' + path;
},
base: function(path) {
return '/' + path;
}
}
});
如何将此提供程序注入配置模块?
how can i inject this provider into a config module?
推荐答案
在 .config
中,您只能使用提供程序(例如 $routeProvider
).在 .run
中,您只能使用服务实例(例如 $route
).你有一个工厂,而不是一个供应商.查看此代码段的三种创建方式:服务、工厂和提供者他们还在 angular 文档 https://docs.angularjs.org/guide/services
In .config
you can only use providers (e.g. $routeProvider
). in .run
you can only use instances of services (e.g. $route
). You have a factory, not a provider. See this snippet with the three ways of creating this: Service, Factory and Provider
They also mention this in the angular docs https://docs.angularjs.org/guide/services
这篇关于在 config() 模块中注入依赖项 - AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!