I'm fairly new to the Angular scene and I'm starting to pull my hair out of over some unexpected behaviour.
I have the following code in my IntegrationsService
queryList(): Observable<ApiList[]> {
if(this.loadingApiList == true) {
return Observable.of(this.queryListResult);
}
this.loadingApiList = true;
return this._http
.get('samples/apiList.json').map( (response: Response) => <ApiList[]> response.json())
.do(data => {
this.queryListResult = data;
});
}
How can I stop this call being executed twice on page load from a route?
A component calls this method and builds a list in the navigation, also another component calls the same service for the following method:
getApiName(IT_ID:number) {
console.log(this.queryListResult);
let obj = this.queryListResult.find(o => o.IT_ID == IT_ID);
if(obj) {
return obj.IT_Name;
}
return false;
}
This works perfectly when I am not using routes, however when I use routes the getApiName returns an undefined result.
I used the resolve object too and that did resolve the problem but then two calls were being made to the json file, this is what I used.
resolve(route: ActivatedRouteSnapshot, state:RouterStateSnapshot):Observable<any>{
return this._service.queryList() // We need this to finish first.
}
I'm really stuck now - I can get it working using the resolve but it's wasteful having two calls to the .json.
What is the correct way of doing this?