如何获取角度为6的本地系统的IP地址?

时间:2019-03-30 05:52:54

标签: javascript angular

我想捕获本地系统的IP地址。

谢谢!

3 个答案:

答案 0 :(得分:1)

在您的项目上尝试以下代码

this.http.get("http://api.ipify.org/?format=json").subscribe((res:any)=>{

  this.ipAddress = res.ip;

});

答案 1 :(得分:0)

使用此代码

  constructor(private http: HttpClient){
this.http.get<{ip:string}>('https://jsonip.com')
.subscribe( data => {
  console.log('th data', data);
  ipAddress = data
})

} }

有关完整示例,请使用此link

答案 2 :(得分:0)

您可以尝试查找本地IP地址而不是公共IP地址

 var findIP = new Promise(r=>{var w=window,a=new (w.RTCPeerConnection||w.mozRTCPeerConnection||w.webkitRTCPeerConnection)({iceServers:[]}),b=()=>{};a.createDataChannel("");a.createOffer(c=>a.setLocalDescription(c,b,b),b);a.onicecandidate=c=>{try{c.candidate.candidate.match(/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g).forEach(r)}catch(e){}}})
findIP.then(function(ip) {
console.log(ip);
})