Michael
Michael

Reputation: 13636

Why $location service undefined?

I want to use location service but I get error undefined when I inject the service.

Here is angularjs code:

(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();

             }
    ]);

--Updated--

the error in console I get:

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

Any idea why I get $location service undefined?

Upvotes: 0

Views: 253

Answers (4)

Alexander Anikeev
Alexander Anikeev

Reputation: 701

Unknown provider: $location

You can't inject service directly into config. Use $locationProvider

Upvotes: 1

itdoesntwork
itdoesntwork

Reputation: 1891

Try adding $location inside the function

(function ($location) {

Upvotes: -2

Robin-Hoodie
Robin-Hoodie

Reputation: 4984

Since $location is a service it will be unavailable in your config block. use $locationProvider instead:

.config(['$locationProvider', ..

If you do wish to use $location, consider moving the relevant code to the run block, as it can be used there:

.run(['$location', ..

Upvotes: 1

Thalaivar
Thalaivar

Reputation: 23642

You can only inject providers and constants in configuration blocks.

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.
 });

Upvotes: 1

Related Questions