2

I have this controller attached to a div:

.controller('ShowInverterConnectController', ['$scope', '$location', function($scope, $location) {

....

}])

I would like to use the $location argument of it.

I am currently doing this:

angular.element('.ng-scope').scope().$apply(function() {
    console.log('test:', angular.element('.ng-scope').scope().$location);
});

But $location is coming out undefined, is there anyway to use $location?

Thanks

2
  • are you using angular.element.... within the controller or out of controller? Commented Jun 29, 2015 at 17:24
  • Thanks @BhojendraNepal its from out of the controller, dfsq's solution below worked great. Commented Jun 29, 2015 at 17:37

1 Answer 1

4

Not sure what you are trying to do with Angular service outside of the app, but I assume you have a good reason, because in many cases this will be considered bad practice. Anyway.

$location is not a property of the scope, so you can't get it like you are trying. However you can use $injector service to retrieve other services like $location:

angular.element('.ng-scope').scope().$apply(function() {
    console.log('test:', angular.element('.ng-scope').injector().get('$location'));
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much for such a fast reply! We have a jquery mobile app we're converting to angularjs for speed but a huge bulk of it cannot be converted within reasonable time, so we are forced to use a mix for this release. Thanks very much.
Would getting $location via injector() be the preferred way over setting a variable within the controller? Like to $scope.loc = $location and then go angular.element('.ng-scope').loc?
I would go with $injector, so you don't pollute scope with properties that are not needed to business logic.
Thank you @dfsq this is some really interesting learning. I will plus your replies asoon as I get 15 rep. Thanks very much also for the fast replies!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.