Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing $location from injector

Tags:

angularjs

I have a simple angularjs application with a single router at the moment :

angular.module('myApp', ['ngRoute'])
            .config(['$routeProvider', function($routeProvider) {
                $routeProvider.when('/editFilter/:id', {templateUrl: '/webapp/template/filterDetail'});
            }])

My goal is to call the editFilter template when I need it. I am trying to retrieve the $location like that :

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

but it fails with

Error: [$injector:unpr] http://errors.angularjs.org/1.2.1/$injector/unpr?p0=%24rootElementProvider%20%3C-%20%24rootElement%20%3C-%20%24location%20%3C-%20%24route

If I call angular.injector(['myApp']).get('$location') it returns true.

Any idea of what I am doing wrong? I have try several workaround but everytime I end up with a very similar error.

like image 320
tibo Avatar asked Dec 19 '13 05:12

tibo


1 Answers

If you would have read the error page you see that you forgot to include certain dependencies. Please don't forget to include them. The one failing now is $rootElement, as you can read in the error. Include this one, and try again to see if other dependencies should be included.

EDIT: OK, I was wrong, after some reading I noticed that this method:

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

creates a new injector instance. This is not what you want I think. So I don't know what is your case but if you are in AngularJS just inject the $injector instance. If not in AngularJS call

$injector = angular.element([DOM element]).injector();
$injector.get('$location');
like image 130
Gecko Avatar answered Sep 21 '22 21:09

Gecko