HDTuto.com

How to get current URL Location in Angularjs?


Here, you will learn how to get current url, hostname, port number, protocol etc using $location variable in angularjs. i gave you full example of getting current full url with port number, protocol etc with angularjs, so just see bellow code how i write example.

we just create "HDTutoApp" app and "HDTutoCtrl" controller for example to getting current full path. So you need to just copy bellow code and run in your desk. So do it that.

Index.html

<!DOCTYPE html>

<html>

<head>

<title>Get current url in Angularjs?</title>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js"></script>

</head>


<body>

<div ng-app="HDTutoApp" ng-controller="HDTutoCtrl">

<p>AngularJs Url of Webpage: {{ weburi }}</p>

<p>Host of Webpage in AngularJs: {{ myhostname }}</p>

<p>AngularJs Port of Webpage: {{ webportno }}</p>

<p>Angular Protocol of Webpage display: {{ webprotocol }}</p>

</div>


<script type="text/javascript">


var HDTutoApp = angular.module('HDTutoApp', []);


HDTutoApp.controller('HDTutoCtrl', function ($scope, $location) {

$scope.weburi = $location.absUrl();

$scope.myhostname = $location.host();

$scope.webportno = $location.port();

$scope.webprotocol = $location.protocol();

});


</script>

</body>


</html>

I hope you found your best solution....




March 26, 2018 | Category : Angular JS
Related Posts