Today I was reading AngularJs documentation about Constants and Values.
A value cannot be injected into configurations, But it can be intercepted by a decorator.
You can learn more about decorators in below blog:
http://www.bennadel.com/blog/2870-using-module-decorator-in-angularjs-1-4.htm
3. Objects can be used inside a constant as long as we have not decide to change any value therein.
So, it is best to stick with values if values are changeable.
As far as I understand, I would like to put together all the reading which I gathered.
1. Constant can be used during config phase of Angular Js and Value is can only be used during run phase.
2. A constant can be injected anywhere as its value cannot be changed that is why it cannot be intercepted by a decorator
For eg:
var app = angular.module('app', []);
app.constant('PI', 3.14159265359);
app.controller('appCtrl', function(PI) {
var radius = 4;
// calculate area of the circle
var area = PI * radius * radius;
});
var app = angular.module('app', []);
app.value('greeting', 'Hello');
app.config(function ($provide) {
$provide.decorator('greeting', function ($delegate) {
return $delegate + ' World!';
});
});
You can learn more about decorators in below blog:
http://www.bennadel.com/blog/2870-using-module-decorator-in-angularjs-1-4.htm
3. Objects can be used inside a constant as long as we have not decide to change any value therein.
So, it is best to stick with values if values are changeable.
// Storing multiple constant values inside of an object
// Keep in mind the values in the object mean they can be modified
// Which makes no sense for a constant, use wisely if you do this
var app = angular.module(‘myApp’, []);
app.constant(‘config’, {
appName: ‘My App’,
appVersion: 2.0,
apiUrl: ‘http://www.google.com?api’
});
// Storing multiple values inside of an object
var app = angular.module(‘myApp’, []);
app.value(‘user’, {
firstName: ’‘,
lastName: ’‘,
email: ’’
});
No comments:
Post a Comment