提供程序只能在“配置”阶段注入,而不能在“运行”阶段注入。相反,诸如$ http之类的服务尚未在“配置”阶段初始化,而仅在“运行”阶段可用。
解决此问题的一个小技巧是在父函数作用域中定义一个变量,以便“ config”和“ run”块都可以访问它:
var routeSegmentProvider = null;myApp.config(["$routeSegmentProvider", function ($routeSegmentProvider) { routeSegmentProvider = $routeSegmentProvider;}]);myApp.run(["$http", function($http) { // access $http and routeSegmentProvider here}]);我不确定您是否会在尝试运行阶段尝试调用$
routeSegmentProvider.setup()时遇到问题,因为我没有尝试过。过去,我可以使用相同的技术向$
httpProvider注册依赖于某些自定义服务的http响应拦截器。



