How to get QueryParam in a factory provider (Angular4)

I want to provide a constant in my QueryParam based application from url.

localhost:4200?flag=true 

In my module providers, I added

 { provide: FLAG, useFactory: FlagFactory, deps: [...] } 

So I'm wondering if there is a way to do this indiscriminately url manually

 function FlagFactory() { return location.search.includes('flag'); } 
+5
source share
1 answer

How about using an angular router?

The router keeps track of the request parameters, so they should be available on your initial '/' route

your root component may have a route that allows this

 constructor(private route: ActivatedRoute) {} ngOnInit() { this.sub = this.route.params.subscribe(params => { this.flag = params['flag']; }); } 
+1
source

Source: https://habr.com/ru/post/1270322/


All Articles