angularjs – 如何注入依赖到module.config(configFn)在角

前端之家收集整理的这篇文章主要介绍了angularjs – 如何注入依赖到module.config(configFn)在角前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在角度,我们可以注入$ routeProvider到配置功能
module.config(function ($routeProvider) {


});

我想把我的服务注入它

module.config(function ($routeProvider,myService) {


});

我确信服务被正确定义,但它抛出一个异常说未知的myService,事件我注入

module.config(function ($routeProvider,$http) {


});

它仍然说未知$ http。

你知道为什么吗?

Modules页,“模块加载和依赖”部分:

Configuration blocks – get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

Run blocks – get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

所以你不能注入自己的服务,或者内置的服务像$ http到config()。改用run()

猜你在找的Angularjs相关文章