Using the $location Service

The $location service provides a wrapper to the JavaScript window.location object. This makes the URL accessible in your AngularJS application. Not only can you get information about the URL, but you also can modify it, changing the location with a new URL or navigating to a specific hash tag.

To add the $location service to a controller or service, you simply need to inject it using the standard dependency injection methods. For example:

app.controller('myController', ['$scope', '$location',                                function($scope, location) {    . . .  }]);

Table 9.5 lists the methods that can be called on the $location service and describes their implementation.

Table 9.5 ...

Get Learning AngularJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.