I'm new to Angular. I'm trying to obtain the Client's IP Address using api.ipify.org API. I'm getting error with the below shown code. Can anyone suggest me how to resolve this issue.
Code from CommonService
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
class IP {
ip: string;
}
export class CommonService {
constructor(private http: HttpClient) { }
strIPObservable: Observable<IP[]>;
getClientIP(){
return this.strIPObservable = this.http.get<IP[]>('http://api.ipify.org/');
}
}
Now below code is from the login component
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { HttpErrorResponse, HttpClient } from '@angular/common/http';
import { Form, FormBuilder } from '@angular/forms';
import { CommonService } from '../CommonService/common.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
providers: [ CommonService ]
})
export class LoginComponent implements OnInit {
LoginForm: Form;
private router: Router;
constructor(private ClsCommon: CommonService) {
}
strIP: any;
ngOnInit() {
this.strIP = this.ClsCommon.getClientIP();
}
OnSubmit(UserID: string, UserPwd: string) {
}
}
Now below code is from login.html
<span>By Logging in, you accept ClearDesk's T&Cs.</span>
<br>Your IP Address is => {{ strIP.ip }}
</mat-card-content>
Below is the error that I'm getting Browser console.
even after adding the CommonService to the providers array... its showing another error