Javascript 使用 Angular JS 将常量注入其他模块配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28416054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inject constant to other modules config using Angular JS
提问by martinoss
I would like to share some variables like base paths throughout my application. These variables needs to be accessible during module configuration. My opinion was, that I can use a constant or provider for that.
我想在整个应用程序中共享一些变量,例如基本路径。在模块配置期间需要访问这些变量。我的意见是,我可以为此使用常量或提供程序。
I've got several modules and each one has it's own routing configuration. In these routing configurations, I want to access some settings for example.
我有几个模块,每个模块都有自己的路由配置。例如,在这些路由配置中,我想访问一些设置。
This is working for the app-module-configuration but not for other module-configurations (for controllers on other modules it does), I always get "Unknown provider: info from myApp.orders".
这适用于应用程序模块配置,但不适用于其他模块配置(适用于其他模块上的控制器),我总是收到“未知提供者:来自 myApp.orders 的信息”。
var myApp = angular.module('myApp', ['myApp.orders']);
myApp.constant('info', {
version : '1.0'
});
myApp.config(function(info) {
console.log('app config: ' + info.version);
});
myApp.controller('MyController', function (info) {
console.log('controller: ' + info.version);
});
var orders = angular.module('myApp.orders', []);
// Remove comments to see it fail.
//orders.config(function(info) {
// console.log('orders config: ' + info.version);
//});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" class="container" ng-controller="MyController">
</div>
I guess I have just missed out a little detail, do you have an idea?
我想我只是错过了一个细节,你有什么想法吗?
回答by ikumen
Your infoconstant is defined in your myAppmodule. If I understand your question correctly, you'd like to use the constants in other modules (e.g. myApp.orders module). If so, then you need to inject myApp into myApp.orders, but it looks like you want to do the reverse. One solution is to decouple the constants into a standalone module, and inject it as a dependency where needed.
您的info常量在您的myApp模块中定义。如果我正确理解您的问题,您希望在其他模块(例如 myApp.orders 模块)中使用常量。如果是这样,那么您需要将 myApp 注入到 myApp.orders 中,但看起来您想要反其道而行之。一种解决方案是将常量解耦到一个独立的模块中,并在需要时将其作为依赖项注入。
angular.module('constants', [])
.constant(...);
angular.module('myApp', ['constants', 'myApp.orders'])
...
angular.module('myApp.orders', ['constants'])
...
回答by Peter Kellner
I don't know if my solution is the most pretty, but I put in my index.html a definition of a CONFIG that I reference from other components. I generate my index.html with server side code so I can set the values when the program starts. That is, I use index.cshtml but it just as easily be index.php or other technology. Here is what my index.html looks like:
我不知道我的解决方案是否最漂亮,但我在 index.html 中放入了我从其他组件引用的 CONFIG 的定义。我用服务器端代码生成我的 index.html 以便我可以在程序启动时设置值。也就是说,我使用 index.cshtml 但它也很容易使用 index.php 或其他技术。这是我的 index.html 的样子:
....
<script type="text/javascript">
var usingMockDataGlobal = true;
</script>
....
<script type="text/javascript">
(function () {
'use strict';
var configData = {
codeCampType: 'angu',
loggedInUsername: 'peter',
mockData: usingMockDataGlobal
};
angular.module('baseApp').constant('CONFIG', configData);
})();
</script>

