AngularJS module.constant (): how to define a constant inside a module?

On the page, I have several Angular modules. For each module, I define a constant that 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, although the constant was defined inside the 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 something else) that is suitable for the module?

Edit : For alternative solutions, could you 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' } 
+7
javascript angularjs
source share
2 answers

Very good question. I could think about it quickly:

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

I do not know how this fits your needs. But I hope this helps.

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

An example of entering a namespace, such as names: http://codepaste.net/5kzfx3

Edit:

In your example, you can use it like this:

 module2.controller('myCtrl', ['$scope', '$http', '$q', ... , 'module2.version'], function( $scope, $http, $q, ..., version ){ // Here I can use the constant 'version' } 
+6
source share

This is because module2 is the module override constant.

You can use moduleName.version as a name, but you cannot use the same name.

+1
source share

All Articles