Javascript 如何在不同的文件中为 Angular JS 定义常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45036138/
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
how to define constants for Angular JS in a different file
提问by Anijit Sau
I want to write several constants for my Angular JS app. I want to write them in a separate file and want to access them.
我想为我的 Angular JS 应用程序编写几个常量。我想将它们写在一个单独的文件中并想访问它们。
I have tried with IIFE (Immediately Invoked Function Expression) like this,
我试过这样的 IIFE(立即调用函数表达式),
constants.js
常量.js
var Constants = (function () {
var allConstants = {
"url": 'abc',
"name": "anijit",
"sn": "sau"
}
return allConstants
})();
console.log('defined constants', Constants)
But when I have tried to access them it show Constants not definederror. Where I have done the wrong thing?
但是当我尝试访问它们时,它显示Constants not defined错误。我哪里做错了?
I want to access them using Constants.urlin the way and I don't want to make any $httpcall or something like that. How to achieve that?
我想以Constants.url这种方式访问它们,我不想$http拨打任何电话或类似的事情。如何做到这一点?
回答by Siddharth Pandey
As such you are using AngularJS, you can use Constant Service. as a constant can be injected anywhere including configuration calls in angularjs application.
因此,您使用的是 AngularJS,您可以使用Constant Service。作为常量可以注入任何地方,包括 angularjs 应用程序中的配置调用。
Also, as the name suggests, the constants are fixed, they get applied before other provide methods. See $provide.constant()for more detail.
此外,顾名思义,常量是固定的,它们在其他提供方法之前应用。有关更多详细信息,请参阅$provide.constant()。
// Storing a single constant value
var app = angular.module('myApp', []);
app.constant('appName', 'My App');
// Now we inject our constant value into a test controller
app.controller('TestCtrl', ['appName', function TestCtrl(appName) {
console.log(appName);
}]);
// Storing multiple constant values inside of an object
// Note: values in the object mean they can be modified
var app = angular.module('myApp', []);
app.constant('config', {
appName: 'My App',
appVersion: 1.0,
apiUrl: 'http://www.facebook.com?api'
});
// Now we inject our constant value into a test controller
app.controller('TestCtrl', ['config', function TestCtrl(config) {
console.log(config);
console.log('App Name', config.appName);
console.log('App Name', config.appVersion);
}]);
回答by Alburkerk
You can use a factory(I personnaly always use storage.factory.js in my projects). Easy to inject everywhere and you can use some functions to setup your constants or change them a little bit if you want.
您可以使用工厂(我个人总是在我的项目中使用 storage.factory.js)。易于在任何地方注入,您可以使用一些函数来设置常量或根据需要对其进行一些更改。
angular.module('app')
.factory('storage', storageFactory);
function storageFactory() {
const data = {
serverAddress : 'http://server.address:port'
};
return data;
}
回答by Eduardo Lima
file 1:
文件 1:
(function () {
'use strict';
angular.module('app', [
'app.constants'
]).value('CONSTANT_EXAMPLE', 'value example')
.value('OTHER_EXAMPLE', 'other example');
})();
file 2:
文件2:
(function () {
'use strict';
angular.module('app.example-use', [])
.factory('example', example);
example.$inject = ['CONSTANT_EXAMPLE']; // and others injections
function example(CONSTANT_EXAMPLE) { // and others injections
function getConstantExample() {
var option = CONSTANT_EXAMPLE;
// use ...
}
return {
getConstantExample: getConstantExample
};
}
})();

