AngularJS module.constant():如何定义只是一个模块内恒定的?只是一个、模块、定义、AngularJS

2023-09-13 04:56:40 作者:永驻你心房

在一个页面上,我有几个角模块。每个模块我定义包含模块的版本的恒定

  VAR模块1 = angular.module('模块1')常数('版本','1.2.3');VAR模块2 = angular.module('模块2')常数('版本','2.0.0')。... 

我虽然不断被定义的在的模块。但是,当我使用常量里面的模块1 的,我得到的值是2.0.0...

有没有一种方法来定义一个常数(或其他任何东西),这是恰当的模块?

修改:为替代解决方案,可以请你解释一下如何在控制器声明中使用它,例如:

  module2.controller('myCtrl',函数($范围,$ HTTP,$ Q,...,版本){    //这里我可以使用常量'版本'} 
angularjs如何在HTML中嵌套循环

解决方案

一个非常好的问题。我能想到的权宜之计此:

  angular.module('模块1')常数(module1.version','1.2.3')。angular.module('模块2')常数(module2.version','2.0.0')。 

我不知道多少适合您的需求。但我希望它能帮助。

的主要问题是在角命名。你不能有服务/常量/值等,他们都将被覆盖相同的名称。我解决了这个问题,命名空间就像我给你上面。

注入空间名称类似的例子: HTTP://$c$cpaste.net/5kzfx3

编辑:

有关你的榜样,你可以使用它像这样:

  module2.controller('myCtrl',['$范围,$ HTTP,$ Q',...,'module2.version'],功能($范围,$ HTTP,$ q,...,版本){    //这里我可以使用常量'版本'} 

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'
}

解决方案

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'
}