I have a question on resolving some data for my Angular 2 route.
I created a resolver service that should return boolean value based on some condition.
However, the problem I am facing with is that in resolve method I need to call this.hotelService.getAll().subscribe(hotels => this.hotels = hotels)
that executes last (after return statement).
This is the code:
resolve(routeSnapshot: ActivatedRouteSnapshot) {
this.hotelService.getAll(this.authenticationService.getUser()
.subscribe((hotels) => {
this.hotels = hotels;
});
return this.hotelService.hasManyCorporations(this.hotels); // => clear true/false value returned
}
As you may notice, in the return statement I am trying to pass this.hotels value to hasManyCorporations method but undefined is present, because this.hotels = hotels
block of code is not yet executed at the time of executing return statement.
How to deal with this situation in which I have a delay of 2-3 seconds? This is my whole resolver service:
@Injectable()
export class HasManyCorporationResolveService implements Resolve<any> {
private hotels: Hotel[];
constructor(
private corporationService: CorporationService,
private authenticationService: AuthenticationService,
private hotelService: HotelService
) { }
resolve(routeSnapshot: ActivatedRouteSnapshot) {
this.hotelService.getAll(this.authenticationService.getUser().id)
.subscribe((hotels) => {
this.hotels = hotels;
});
return this.hotelService.hasManyCorporations(this.hotels); // clear true/false value returned
}
}