Reputation: 1624
I have application which is hosted locally and online. I have a lot of api calls, and we have need to change the prefix of url. I have set value url with both url addressed, one is needed locally and one online. So I have:
//.value('urlPath', '')
.value('urlPath', 'http://online-website:8888/')
In this configuration, there is commented one and second not. My question is, how to change urlPath dynamically, so there is no need by developers to comment and uncomment each time they deploy to server. I know i can use $location service, and if not, I can use javascript raw function to determine which host the website is located. So I can make condition like this:
var host = $location.host();
if ( host.indexOf("online-website") > -1)
//set value to online-website;
else
//set value to localhost
I assume I can use this code snippet somehow in the beginning, like in .run module method. But how to achieve this by setting Angular's Value conditionally? My question is if value has method to set dynamically it's value, how to reference it? Or is there another solution, except creating service or inheriting controller. I have a lot of controllers which are using value object, so the best solution would be with value.
Also, if it is not possible, what is the difference between value and constant?
I wanted to set it even dynamically in run method. it's defined after value is defined. I am setting value dynamically by:
var host = $location.host();
console.log('host is: ',host);
if ( host.indexOf("website-online") > -1)
{urlPath = '';console.log('1');}
else
{urlPath = 'http://website-online:8888/';console.log('2',urlPath);}
On localhost console logs 2 and website-online, but api call is going to localhost. So value doesn't changed. When it is created, how to change value and when dynamically, so in each controller injected value during view loading would be fine?
Upvotes: 0
Views: 949
Reputation: 222503
In your example urlPath
is scalar. This means that
urlPath = ...
is assigned to local variable, while 'urlPath' service remains intact. Use
.value('urlPath', { url: 'http://online-website:8888/' })
and
urlPath.url = ...
instead.
Upvotes: 1