javascript AngularJS - 如何在我自己的服务中使用 .config() 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19341698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS - How can I use .config() method with my own services?
提问by Kevin Beal
I recently used the .config()
method on an angular module I was developing in order to use AngularJS's routes. It's looked something like:
我最近.config()
在我正在开发的 angular 模块上使用了该方法,以便使用 AngularJS 的路由。它看起来像:
myModule.config([
'$locationProvider',
'$routeProvider',
function ($locationProvider, $routeProvider) {
$locationProvider.html5Mode(false);
$locationProvider.hashPrefix('!');
$routeProvider.when('/', {
controller: 'myCtrl'
});
}]);
How does this method work? Can I configure my own services using this method? Should I?
这种方法是如何工作的?我可以使用这种方法配置我自己的服务吗?我是不是该?
回答by Davide Icardi
During the config
phase, only providers can be injected. So I think that you can create a custom provider and then configure it during the config
phase.
在该config
阶段,只能注入提供者。所以我认为您可以创建一个自定义提供程序,然后在该config
阶段进行配置。
See this documentation (already mentioned by Brandon Tilley in a comment): https://github.com/angular/angular.js/wiki/Understanding-Dependency-Injection#configuring-providers
请参阅此文档(Brandon Tilley 在评论中已经提到):https: //github.com/angular/angular.js/wiki/Understanding-Dependency-Injection#configuring-providers
Basically angularjs first invoke the config method and then invoke the run method. During config only providers are available. A provider can then be used to create service instance.
基本上 angularjs 首先调用 config 方法,然后调用 run 方法。在配置期间,只有提供程序可用。然后可以使用提供者来创建服务实例。