加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – Angular在配置或提供程序中将$http注入运行

发布时间:2020-12-17 08:00:24 所属栏目:安全 来源:网络整理
导读:我在我的角应用程序中使用angular-route-segment并尝试从json feed配置段. 我有这个问题,因为我无法弄清楚如何将$http注入app.config函数.使用未知提供程序失败:$http myApp.config(["$http","$routeSegmentProvider",function ($http,$routeSegmentProvide
我在我的角应用程序中使用angular-route-segment并尝试从json feed配置段.

我有这个问题,因为我无法弄清楚如何将$http注入app.config函数.使用未知提供程序失败:$http

myApp.config(["$http","$routeSegmentProvider",function ($http,$routeSegmentProvider) {
   /* setup navigation here calling $routeSegmentProvider.when a number of times */
}

因此,我没有在配置中注入$http,而是尝试将$routeSegmentProvider注入myApp.run

myApp.run(["$http","$routeSegment",function($http,$routeSegment) {
    /* can use $http here to get data,but $routeSegment is not the same here */
    /* $routeSegment does not have the when setup method */
}]);

我也试过了

myApp.run(["$http",$routeSegmentProvider)

但是我得到了未知的提供者:$routeSegmentProviderProvider< - $routeSegmentProvider

提供者只能在“配置”阶段而不是“运行”阶段注入.相反,$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()时遇到问题,因为我没有尝试过.在过去,我能够使用相同的技术来注册依赖于某些自定义服务的http响应拦截器和$httpProvider.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读