为什么$ location服务未定义?

时间:2016-09-26 11:18:03

标签: angularjs

我想使用位置服务但是在注入服务时我得到错误未定义。

这是angularjs代码:

(function () {
      "use strict";

       angular.module("workPlan", ['ui.router',
                                        'geomindCommon',
                                        'templates',
                                        'lookups',
                                        "ngAnimate",
                                        'ngTouch',
                                        'ui.grid',
                                        'ui.grid.expandable',
                                        'ui.grid.selection',
                                        'ui.grid.pinning',
                                        'ui.grid.resizeColumns',
                                        'ui.bootstrap',
                                        'damageEvent'])

           .config([
                    "$location",
                    "$stateProvider",
                    "$urlRouterProvider",
                    function ($location, $stateProvider, $urlRouterProvider) {

                        $urlRouterProvider.otherwise("/MainPage");
                        $urlRouterProvider.when('/MainPage/onDevice', '/sites/list');//if request comes from device
                        $urlRouterProvider.when('/MainPage/inspectorArea',    '/inspectorArea');
                     //var absUrl = $location.absUrl();

             }
    ]);

- 更新 -

我得到的控制台错误:

angular.js:4587 Uncaught Error: [$injector:modulerr] Failed to instantiate module dashboard due to:
Error: [$injector:modulerr] Failed to instantiate module sites due to:
Error: [$injector:modulerr] Failed to instantiate module workPlan due to:
Error: [$injector:unpr] Unknown provider: $location
http://errors.angularjs.org/1.5.5/$injector/unpr?p0=%24location
    at http://localhost/Playground/Scripts/angular.js:68:12
    at http://localhost/Playground/Scripts/angular.js:4458:19

我知道为什么我的$location服务未定义?

4 个答案:

答案 0 :(得分:1)

  

未知提供商:<root> <label>label (1)</label> <label>(label) (1)</label> <label>1</label> <label>(This is)(a (label))</label> <label>This (is) (a label)</label> </root>

您无法直接将服务注入配置。使用$location

答案 1 :(得分:1)

您只能在配置块中注入providersconstants

angular.module('aop', []).
 config(function(injectables) {
   //the injectables here should be provider or constants. $locationProvider
 }).
 run(function(injectables) { 
   //the injectables here can be instance... ie.. $location service.
 });

答案 2 :(得分:1)

由于$location是一项服务,因此您的config广告资源将无法使用该服务。改为使用$locationProvider

.config(['$locationProvider', ..

如果您确实希望使用$location,请考虑将相关代码移至run块,因为它可以在那里使用:

.run(['$location', ..

答案 3 :(得分:-2)

尝试在函数

中添加$ location
(function ($location) {