Reputation: 19
It is possible that I can define a constant in angularjs like this:
JS Code
app.controller('testController', function($scope) {
$scope.TEST_CONST= {
ASC: 1,
DESC: 2
};
$scope.doSomething = function() {
// using constant
if ($scope.TEST_CONST.ASC) {
// do something
}
}
});
HTML code:
// using in a template
<p ng-if="TEST_CONST.ASC">abc<p>
As you can see, in the js code, when I use the constant, the writting of constant definition is not concise, because I must add a $scope before the constant. It's much better if I just write TEST_CONST.ASC like in the template file.
Is there any better way to define a constant?
Upvotes: 0
Views: 54
Reputation: 1919
Basically, you need $scope
to make your variable visible in the template. So there is no way to avoid using it. On the other hand, AngularJS has a special, more convenient, syntax for that purpose:
angular
.module('my.module')
.constant('TEST_CONST', {
ASC: 1,
});
But still, you will need to inject it into the controller:
app.controller('testController', function($scope, TEST_CONST) {}
And then make visible in a template:
$scope.TEST_CONST = TEST_CONST;
Upvotes: 1