Search code examples
javascriptangularjsangularjs-injector

How do i use $location in a normal function


I have a angular app where half is angular js and half is plane js like ajax. So how do i use $location inside that function without passing it as a function parameter.

function x() {
   $location.path('/error').replace();
}

Now i wish to know how to use $location inside a normal javascript function. Basically i have a function that is being called from at least 20 places. In that function i wish to do replacestate.


Solution

  • You need use angular.injector that will provide and access to provider from outside.

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

    For more information Refer this SO Answer

    Update

    You could directly apply injector on element, that will give access to all the service present inside that module.

    angular.element('body').injector().get('$location'); //body can replace by your element.