AngularJs : How to get client local IP address

/ / 2 Comments

Angularjs Get local IP Address: Here in this article will see how to get client IP address (local ip) in AngularJs application. For this, we use angularjs $http service to make an ajax call, which in response gives us the Client IP AddressHostnameCity , RegionGeo Location (Latitude, Longitude) , Country  and name of organization and other details.

Basically, we call https://ipinfo.io/json which display IP Address.

Code to show client IP Address in Angularjs.

HTML Markup: Here added a span tag with an attributeng-bind with value as ip, which displays the IP address.
<body ng-app="myApp">
<body ng-app="myApp">
<div ng-controller="myip"> <span ng-bing="ip"></span>
</div> </body>
AngularJS Code: Here we use $http service to make an HTTP request, which in response gives the IP address and other information. The final code looks like as written below.
<script>
	var app = angular.module('myApp', []);
	app.controller('myip', function($scope, $http) {
		$http.get("https://ipinfo.io/json").then(function (response) 
		{
			$scope.ip = response.data.ip;
		});
});
</script>

Conclusion: In Angularjs using $http service we call (https://ipinfo.io/json) and get the client information i.e local ip address. Here in $scope.ip  we have saved the client Ip Address.

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to do a favor, then Buy me a coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

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

2 Comments

  1. Andrzej 06/20/2018 13:08:31
    The proper URL of GET request should be: https://ipinfo.io/json
  2. Satinder singh 07/01/2018 09:16:35
    Hi Andrzej, Great observation thanks for it