AngularJS $location Service


The $location service has methods which return information about the location of the current web page.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
<p>The url of this page is:</p>
<h3>{{myUrl}}</h3>
</div>

<p>This example uses the built-in $location service to get the absolute url of the page.</p>
<p>This html file is on my computer's hard drive at the following location on my C drive:</p>
<p>C:\Sites\angular\appLocation.html</p>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $location) {
    $scope.myUrl = $location.absUrl();
});
</script>

</body>
</html>

The output looks like this, in the Chrome browser

Leave a comment

Your email address will not be published. Required fields are marked *