在角度,我们可以注入$ 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()。