admin管理员组文章数量:1200373
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.
I have tried with IIFE (Immediately Invoked Function Expression) like this,
constants.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 defined
error. Where I have done the wrong thing?
I want to access them using Constants.url
in the way and I don't want to make any $http
call or something like that. How to achieve that?
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.
I have tried with IIFE (Immediately Invoked Function Expression) like this,
constants.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 defined
error. Where I have done the wrong thing?
I want to access them using Constants.url
in the way and I don't want to make any $http
call or something like that. How to achieve that?
- 1 Read stackoverflow.com/questions/24831323/angularjs-constants – Satpal Commented Jul 11, 2017 at 13:46
- Additional examples: bguiz.github.io/js-standards/angularjs/constants – terpinmd Commented Jul 11, 2017 at 13:50
3 Answers
Reset to default 16As such you are using AngularJS, you can use Constant Service. as a constant can be injected anywhere including configuration calls in angularjs application.
Also, as the name suggests, the constants are fixed, they get applied before other provide methods. See $provide.constant() for more detail.
// 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);
}]);
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.
angular.module('app')
.factory('storage', storageFactory);
function storageFactory() {
const data = {
serverAddress : 'http://server.address:port'
};
return data;
}
file 1:
(function () {
'use strict';
angular.module('app', [
'app.constants'
]).value('CONSTANT_EXAMPLE', 'value example')
.value('OTHER_EXAMPLE', 'other example');
})();
file 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
};
}
})();
本文标签: javascripthow to define constants for Angular JS in a different fileStack Overflow
版权声明:本文标题:javascript - how to define constants for Angular JS in a different file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738612815a2102729.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论