In my angular app I have two services, one deals with the http layer, and the other is simply a route guard to implement the canActivate
method related to the status of the first service. For example:
backend.service:
@Injectable()
export class BackendService {
private data: any;
constructor(private http: Http) {
console.log('backend service constructor hit');
this.data = 'initial';
}
getData() {
console.log(this.data);
return this.data;
}
setData(data) {
// http stuff
this.data = data;
}
}
routeguard.service:
@Injectable()
export class RouteguardService implements CanActivate {
constructor(private backendService: BackendService) { }
canActivate() {
return this.backendService.getData() !== 'initial';
}
}
I believe I am providing one of the services in the wrong location? Currently I have both services in the providers
array of my app.module
. But I can tell with the console.log statements that the backend.service
is being new'd up separately when it gets called as part of the routing versus when a component uses it, so the data is different and the data always comes back as 'initial' when checked in the canActivate method despite having it set to something else from a component. Hope I explained that clearly, I'm still new to angular.
Do I need to provide one of the services in a different location or am I doing something else wrong entirely? Thanks for any pointers.