Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Constants vs Values

Tags:

angularjs

As far as I understand the documentation, the only concrete difference between a Constant and a Value is that a Constant can be used during the apps config phase, whereas a Value is only available during the run phase.

I am curious as to why Values are needed at all in this case? Aren't they really just limited Constants?

like image 364
csvan Avatar asked May 19 '15 13:05

csvan


People also ask

What is the difference between value and constant?

Constants can put anywhere whereas Values cannot be added anywhere. Also constants cannot be intercepted by decorators whereas values can be intercepted by decorators.

What is constants in AngularJS?

Constant are like services in AngularJS in which we can define our globally data. It is declare using "constant" keyword. As we define our app-keys in Web.

What is the meaning of value in Angular?

Definition and Usage The ng-value directive sets the value attribute of a input element, or a select element.

What is $$ in AngularJS?

The $ in AngularJs is a built-in object.It contains application data and methods.


2 Answers

A constant can be injected anywhere.

A constant can not be intercepted by a decorator, that means that the value of a constant should never be changed.

var app = angular.module('app', []);  app.constant('PI', 3.14159265359);  app.config(function(PI){     var radius = 4;     //PI can be injected here in the config block     var perimeter = 2 * PI * radius; });  app.controller('appCtrl', function(PI) {     var radius = 4;     // calculate area of the circle     var area = PI * radius * radius;  }); 

Value differs from constant in that value can not be injected into configurations, but it can be intercepted by decorators.

var app = angular.module('app', []);  app.value('greeting', 'Hello');  app.config(function ($provide) {     $provide.decorator('greeting', function ($delegate) {         return $delegate + ' World!';     }); }); 
like image 82
Artem Petrosian Avatar answered Oct 09 '22 07:10

Artem Petrosian


The difference between value and constant is that a value specified using constant is available during the configuration phase.

Well it’s the same for value and constant. constant is available from the configuration phase and value is not.

The other difference is as the name implies you can’t change the value of a constant. The first value you assign it is the value it keeps, if you try to assign it a different value later it will be ignored.

Here’s an example:

mod.value("myValue", "First Assignment");  mod.value("myValue", "Second  Assignment");  mod.constant("myConstant", "First Assignment");  mod.constant("myConstant", "Second Assignment");  mod.controller("MyController", function(myValue, myConstant) {      console.log("myValue: " + myValue);      console.log("myConstant: " + myConstant); }); 

Console output:

myValue: Second Assignment  myConstant: First Assignment 
like image 39
Bhupendra Gupta Avatar answered Oct 09 '22 07:10

Bhupendra Gupta