MOCKSTACKS
EN
Questions And Answers

More Tutorials









Angular Constants

Create your first constant


angular

 .module('MyApp', [])
 .constant('VERSION', 1.0);


Your constant is now declared and can be injected in a controller, a service, a factory, a provider, and even in a config method:

angular

 .module('MyApp')
 .controller('FooterController', function(VERSION) {
 this.version = VERSION;
 });
<footer ng-controller="FooterController as Footer">{{ Footer.version }}</footer>

Use cases


There is no revolution here, but angular constant can be useful specially when your application and/or team starts to grow ... or if you simply love writing beautiful code!

Refactor code. Example with event's names. If you use a lot of events in your application, you have event's names a little every where. A when a new developer join your team, he names his events with a different syntax, ... You can easily prevent this by grouping your event's names in a constant:

angular

 .module('MyApp')
 .constant('EVENTS', {
 LOGIN_VALIDATE_FORM: 'login::click-validate',
 LOGIN_FORGOT_PASSWORD: 'login::click-forgot',
 LOGIN_ERROR: 'login::notify-error',
 ...
 });


angular

 .module('MyApp')
 .controller('LoginController', function($scope, EVENT) {
 $scope.$on(EVENT.LOGIN_VALIDATE_FORM, function() {
 ...
 });
 })


... and now, your event's names can take benefits from autocompletion ! Define configuration. Locate all your configuration in a same place:

angular

 .module('MyApp')
 .constant('CONFIG', {
GoalKicker.com – AngularJS Notes for Professionals 57
 BASE_URL: {
 APP: 'http://localhost:3000',
 API: 'http://localhost:3001'
 },
 STORAGE: 'S3',
 ...
 });


Isolate parts. Sometimes, there are some things you are not very proud of ... like hardcoded value for example. Instead of let them in your main code, you can create an angular constant.

angular

 .module('MyApp')
 .constant('HARDCODED', {
 KEY: 'KEY',
 RELATION: 'has_many',
 VAT: 19.6
 });


... and refactor something like

$scope.settings = {
 username: Profile.username,
 relation: 'has_many',
 vat: 19.6
}


to
$scope.settings = {
 username: Profile.username,
 relation: HARDCODED.RELATION,
 vat: HARDCODED.VAT
}



Conclusion

In this page (written and validated by ) you learned about AngularJS Constants . What's Next? If you are interested in completing AngularJS tutorial, your next topic will be learning about: AngularJS Custom filters with ES6.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.