userMod2 userMod2 - 1 year ago 165
AngularJS Question

Angular - Constant Variable Decided Conditionally

In my Angular app I have defined a constant such as:

var app = angular.module('app', ['ngTagsInput'])
.constant('API', '')

However I want this API URL to be dependant on a constant I have set above, namely:

var WORKING_ON_ENV = "dev";

So I've tried to implement this using the following - however its not working - is this actually possible with Angular constants?:

.constant('API', function() {
if (WORKING_ON_ENV == "prod") {
return ''
} else {
return ''

Any advice appreciated.


Answer Source

You can use a ternary:

var app = angular.module('app', ['ngTagsInput'])
   .constant('API', (WORKING_ON_ENV == "prod" ? '' : ''))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download