I was able to do this by using Angular's resolve. Resolve allows you to access the route and query params BEFORE your route component is loaded.
In your case, I would create a resolver class that looks something like this:
@Injectable()
export class FlagRouteResolver implements Resolve<any> {
constructor(private flagService: FlagService) {}
resolve(route: ActivatedRouteSnapshot) {
this.flagService.isFlagged = route.queryParamMap.get('flag');
// Need to return an observable for all resolve() methods
return Observable.of(null);
}
}
And your AppModule should look something like this:
@NgModule({
imports: [
RouterModule.forRoot([{
path: 'flagRoute',
component: FlagComponent,
resolve: { test: FlagRouteResolver }
}]),
],
providers: [
FlagService,
FlagRouteResolver
],
declarations: [
FlagComponent
]
})
So now, within your route component, you should be able to access that value immediately in the constructor like so:
constructor(private flagService: FlagService) {
this.isFlagged = this.flagService.isFlagged;
}
But if you'd still like to inject it, you can also do it like so:
providers: [{
provide: FLAG,
useFactory: (flagService: FlagService) => {
return this.flagService.isFlagged;
},
deps: [FlagService]
}]