如何在不同的文件中为 Angular JS 定义常量
how to define constants for Angular JS in a different file
我想为我的 Angular JS 应用程序编写几个常量。我想把它们写在一个单独的文件中并想访问它们。
我试过像这样使用 IIFE(立即调用函数表达式),
constants.js
var Constants = (function () {
var allConstants = {
"url": 'abc',
"name": "anijit",
"sn": "sau"
}
return allConstants
})();
console.log('defined constants', Constants)
但是当我尝试访问它们时显示 Constants not defined
错误。我哪里做错了?
我想以这种方式使用 Constants.url
访问它们,我不想进行任何 $http
调用或类似的调用。如何实现?
因此您正在使用 AngularJS,您可以使用 Constant Service。作为常量可以注入任何地方,包括 angularjs 应用程序中的配置调用。
此外,顾名思义,常量是固定的,它们在其他提供方法之前得到应用。有关详细信息,请参阅 $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);
}]);
您可以使用 factory(我个人总是在我的项目中使用 storage.factory.js)。易于在任何地方注入,您可以使用一些函数来设置常量或根据需要稍微更改它们。
angular.module('app')
.factory('storage', storageFactory);
function storageFactory() {
const data = {
serverAddress : 'http://server.address:port'
};
return data;
}
文件 1:
(function () {
'use strict';
angular.module('app', [
'app.constants'
]).value('CONSTANT_EXAMPLE', 'value example')
.value('OTHER_EXAMPLE', 'other example');
})();
文件 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
};
}
})();
我想为我的 Angular JS 应用程序编写几个常量。我想把它们写在一个单独的文件中并想访问它们。
我试过像这样使用 IIFE(立即调用函数表达式),
constants.js
var Constants = (function () {
var allConstants = {
"url": 'abc',
"name": "anijit",
"sn": "sau"
}
return allConstants
})();
console.log('defined constants', Constants)
但是当我尝试访问它们时显示 Constants not defined
错误。我哪里做错了?
我想以这种方式使用 Constants.url
访问它们,我不想进行任何 $http
调用或类似的调用。如何实现?
因此您正在使用 AngularJS,您可以使用 Constant Service。作为常量可以注入任何地方,包括 angularjs 应用程序中的配置调用。
此外,顾名思义,常量是固定的,它们在其他提供方法之前得到应用。有关详细信息,请参阅 $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);
}]);
您可以使用 factory(我个人总是在我的项目中使用 storage.factory.js)。易于在任何地方注入,您可以使用一些函数来设置常量或根据需要稍微更改它们。
angular.module('app')
.factory('storage', storageFactory);
function storageFactory() {
const data = {
serverAddress : 'http://server.address:port'
};
return data;
}
文件 1:
(function () {
'use strict';
angular.module('app', [
'app.constants'
]).value('CONSTANT_EXAMPLE', 'value example')
.value('OTHER_EXAMPLE', 'other example');
})();
文件 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
};
}
})();