0

Is there any way to achieve this?

var app = angular.module('myApp', []);

app.constant('config', {
    "appName": "My App",
    "appVersion": "2.0",
    "url": "http://www.myservices.com",
    "apiUrl1": url + "/rest_service_1",
    "apiUrl2": url + "/rest_service_2",
    "apiUrl3": url + "/rest_service_3"
});

I want to avoid repeat the content of url in all my constants. Any suggestion?

napstercake
  • 1,815
  • 6
  • 32
  • 57
  • I would use $rootScope http://stackoverflow.com/questions/18880737/how-do-i-use-rootscope-in-angular-to-store-variables – Timothy Aug 31 '15 at 15:33
  • possible duplicate of [Can a JavaScript object property refer to another property of the same object?](http://stackoverflow.com/questions/3173610/can-a-javascript-object-property-refer-to-another-property-of-the-same-object) – sp00m Aug 31 '15 at 15:35
  • @Timothy in some cases you would use a constant into a config block – napstercake Aug 31 '15 at 15:40
  • I think you want to make a factory then... We did something like this at my old job, let me see if I can throw a plnkr together real quick... – Timothy Aug 31 '15 at 15:43

1 Answers1

0

You could use a factory to do it.

var app = angular.module('plunker', []);
app.factory('constants', function() {
  return {
    url: 'http://google.com'
  };
});

app.controller('MainCtrl', function($scope, constants) {
  console.log(constants.url);
});

Here's a plnkr: http://plnkr.co/edit/46rjTxb9Lz73S5Ol10Z6?p=preview

Timothy
  • 1,198
  • 3
  • 10
  • 30