4

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

Mohaideen
  • 209
  • 1
  • 2
  • 13
  • `Need to send system ip in every rest call` why? That's sort of handled for you by the web anyway. – TZHX May 01 '17 at 06:46
  • 1
    Why do you need to send your system IP in every REST call? The destination server should be able to view that information already, as part of the TCP/IP protocol that HTTP uses. – Soron May 01 '17 at 06:48
  • http://stackoverflow.com/a/35123097/5621827 may help – jitender May 01 '17 at 07:04

2 Answers2

8

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"

georgeawg
  • 48,608
  • 13
  • 72
  • 95
0
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);
        }
        }
  • Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Jul 24 '18 at 07:44
  • 1
    This is an Angular 2+ solution. The question was for AngularJS. – georgeawg Aug 28 '18 at 13:25