Mohaideen
Mohaideen

Reputation: 209

How to get system ip address by using angularjs

Need to get System IP Address from angularjs code.

I have searched in google. Everyone are suggesting third party from js.

So i dont want to make a call to get IP?

Is there any way to get IP from angularjs?

My actual scenario is.

Need to send system ip in every rest call

Upvotes: 4

Views: 20741

Answers (2)

sudha priya
sudha priya

Reputation: 41

It will work in angular 2/4/6/...

 import { HttpClient } from '@angular/common/http';

    export class AppComponent implements OnInit {
            ipAddress: any;
            constructor(private http: HttpClient ) {
            this.http.get('https://jsonip.com').subscribe((ipOfNetwork) => 
           this.ipAddress =  ipOfNetwork['ip']);
        }

        ngOnInit() {
        console.log(this.ipAddress);
        }
        }

Upvotes: 0

georgeawg
georgeawg

Reputation: 48948

How to get system ip address by using AngularJS

Use the location service at freegeoip.net

angular.module("myApp",[]).run(function($rootScope, $http) {
  var url = "//freegeoip.net/json/";
  $http.get(url).then(function(response) {
    console.log(response.data.ip);
    $rootScope.ip = response.data.ip;
  });
});
<script src="https://unpkg.com/angular/angular.js"></script>
<div ng-app="myApp">
  IP address = {{ip}}
</div>

Update

This API endpoint is deprecated and will stop working on July 1st, 2018. For more information please visit: https://github.com/apilayer/freegeoip#readme"

Upvotes: 8

Related Questions