7

On a page, I have several Angular modules. For each module I define a constant which contains the version of the module.

var module1 = angular.module('module1').constant('version', '1.2.3');
var module2 = angular.module('module2').constant('version', '2.0.0');
...

I though a constant was defined inside a module. But when I use the constant inside module1, the value I get is '2.0.0'...

Is there a way to define a constant (or anything else) which is proper to a module ?

Edit: for alternative solutions, could you please explain how to use it, for example in a controller declaration ?

module2.controller('myCtrl', function( $scope, $http, $q, ..., version ){
    // Here I can use the constant 'version'
}
Eria
  • 2,653
  • 6
  • 29
  • 56
  • see if you import `module2` as a dependency of `module1`, in that case the `version` constant may get overriden – nhd Jan 18 '16 at 15:01
  • 2
    Possible duplicate of [Modules and namespace / name collision in AngularJS](http://stackoverflow.com/questions/13406791/modules-and-namespace-name-collision-in-angularjs) – Zakaria Jan 18 '16 at 15:09

2 Answers2

6

A very good question. I could think of a quick fix for this:

angular.module('module1').constant('module1.version', '1.2.3');
angular.module('module2').constant('module2.version', '2.0.0');

I don't know how much it suits your needs. But I hope it helps.

The main problem is the naming in Angular. You cannot have the same name for services/constants/values etc. They will be overwritten. I solved this problem with "namespaces" like I showed you above.

Example of injecting namespace like names: http://codepaste.net/5kzfx3

Edit:

For your example, you could use it like so:

module2.controller('myCtrl', ['$scope', '$http', '$q', ... , 'module2.version'], function( $scope, $http, $q, ..., version ){
    // Here I can use the constant 'version'
}
Andrei CACIO
  • 2,101
  • 15
  • 28
  • How can I inject a constant having a dot in its name ? (for example in a controller inside my module) – Eria Jan 18 '16 at 15:06
  • angular.module().service(['module1.version'], function(version){}); You can have aliases. It's also a best practice to inject dependencies like this. Example: http://codepaste.net/5kzfx3 – Andrei CACIO Jan 18 '16 at 15:07
  • Doing what you suggest for the injection give me the following error : `Unknown provider: module1.infoProvider <- module1.info <- myCtrl` – Eria Jan 18 '16 at 15:38
  • this requires more explanation. why can't there be the same name for providers when they belong to different modules? – jkris Jan 18 '16 at 15:42
  • Because Angular wouldn't know what service to inject. If you have 2 modules with 2 services with the same name and you inject a service, Angular doens't konw which service to inject. Angular DI works on names. It searches the name of the dependency, not it's module of origin. – Andrei CACIO Jan 18 '16 at 15:56
1

That happens because module2 is overriding module1 constant.

You can use moduleName.version as a name, but it's not possible to use the same name.

Serginho
  • 7,291
  • 2
  • 27
  • 52