Suppose that I have a global constant that need to be accessible in every angular module, what is the advisable approach to declare the constant. I have three approach in my mind but i not sure which to be used.
Appreciate if anyone could point out what is the benefit using Approach 2 compare to Approach 1 in this condition.
Approach 1 (pure js constant):
var jsConstant = {
constant1: 'constant1',
constant2: 'constant2'
};
angular.module('mainApp').controller(['$scope', function($scope) {
$scope.constant1 = jsConstant.constant1;
$scope.constant2 = jsConstant.constant2;
}]);
Approach 2 (pure angular constant)
angular.module('mainApp').constant('angularConstant', {
'constant1': 'constant1',
'constant2': 'constant2'
});
angular.module('mainApp').controller(['myConstant', '$scope', function(myConstant, $scope) {
$scope.constant1 = angularConstant.constant1;
$scope.constant2 = angularConstant.constant2;
}]);
Approach 3 (mixture of js and angular constant)
var jsConstant = {
constant1: 'constant1',
constant2: 'constant2'
};
angular.module('mainApp').constant('angularConstant", {
'constant1': jsConstant.constant1;
'constant2': jsConstant.constant2;
});
angular.module('mainApp').controller(['myConstant', '$scope', function(myConstant, $scope) {
$scope.constant1 = angularConstant.constant1;
$scope.constant2 = angularConstant.constant2;
}]);
angular.module('myApp').constant('Cons',{}). And then you may do injection in your controllers. And yo can create separate file with constants.