json4
json4

Reputation: 11

Why can't I get an $location from the $injector?

I am new to angular and trying to use the $location service outside of the scope using $injector.

When I run the following code I keep getting an error

   angular.injector(['myApp']).get('$location');

Any suggestions?

Upvotes: 1

Views: 341

Answers (1)

georgeawg
georgeawg

Reputation: 48968

The $location service is not part of the myApp module, it is part of the internal ng module.

To get the $http service, use:

var $http = angular.injector(['ng']).get("$http");

The $location service depends on the $rootElement service which is created at bootstrap time. To get the $location service use:

var $rootElementModule = function($provide) {
    $provide.value('$rootElement', angular.element(document))
}

var $location = angular.injector(['ng',$rootElementModule]).get("$location");

Upvotes: 2

Related Questions