2

My question is similar to this one here, I'm creating a new question because I can't comment: How to store the root url of api in angular 4 app?

I like Mike Kovetsky`s answer but I'm not too sure on how to implement in my case, basically I want the user to be able to change the endpoints (pre selected through config files).

So, how can I change API_URL programmatically?

  • 1
    What is "API URL" exactly? – Günter Zöchbauer Jan 19 '18 at 14:17
  • it's the InjectionToken with the API URL from Mike's answer. It's a fixed URL based on the environment (dev, prod), I want to have multiple urls in dev & prod and have the ability to switch between them. Like this: {provide: API_URL, useValue: environment.apiUrl} – Rafael Farias Jan 19 '18 at 14:41
  • It would be easier to use a service that provides that URLs using getters and then depending on a property of the service return different values. Then you just need to set that property to configure the URLs. You can also use something like https://stackoverflow.com/questions/35655361/angular2-how-to-load-data-before-rendering-the-component/35655492#35655492 to ensure it is set before any code in services or components is executed. – Günter Zöchbauer Jan 19 '18 at 14:53

1 Answers1

1

I've end up setting up logic inside my interceptor to switch the endpoints, not sure if it's the best solution but will work for the moment.