In my service, I want to wait for until local variable baseurl is not initialized before making another http request.
Below is my service code:
@Injectable()
export class CoursesService {
baseUrl;
constructor(private http: Http) {
if(this.baseUrl != undefined){
this.getJSON().subscribe(data =>
this.baseUrl=data,
error => console.log(error)
);
}
}
public getJSON(): Observable<any> {
return this.http.get("assets/apiDetails.json")
.map((res:any) => res.json());
}
getCourses(){
return this.http.get(this.baseUrl+"/courses")
.map((res:any) => res.json());
}
}
As you can see getCourses method uses baseUrl variable, so When I will call getCourses method , I want to wait until baseUrl is not initialized.
I have tried to use ngOnInit but it not get called in Injectable type class.