I am trying to show a loading spinner before a Service is called. If the service fails, it should hide the loading spinner.
I am able to show the spinner, but not able to hide it, because the component variables are not accessible in the subscribe method. Is there any way around this?
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { NgxSpinnerService } from 'ngx-spinner';
import { FoodService } from '../../../services/food.service';
import { Food } from '../../../interfaces/Food';
@Component({
selector: 'app-food',
templateUrl: './food.component.html',
styleUrls: ['./food.component.css']
})
export class FoodComponent implements OnInit {
foodContents$: Observable<Food>
public foodPageId : any;
public spinnerStatus: boolean = false;
public foodRequest;
constructor(private foodService:FoodService, private route: ActivatedRoute, private spinner: NgxSpinnerService) {
this.route.params.subscribe(res => this.foodPageId = res.id);
}
ngOnInit() {
if(!this.foodRequest)
{
this.spinner.show();
this.spinnerStatus = true;
this.foodContents$ = this.foodService.GetFoodPageContent(this.foodPageId);
this.foodContents$.subscribe({
error(err) {
/*Not able to access spinner and spinnerStatus variables.
How to access these?*/
this.spinner.hide();
this.spinnerStatus = false;
}
});
}
}
}