top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is difference between value and constant?

0 votes
188 views
What is difference between value and constant?
posted Nov 11, 2017 by Latha

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Value and Constant are simple objects which are used to share data globally with in a module.
Value - A value can be a number, string, date-time, array or object. You can also register a function as a value.
Values are typically used as configuration which is injected into factories, services or controllers.

//define module
var app = angular.module('app', []);
//define value
app.value("numberValue", 100);
app.value("stringValue", "dotnet-tricks.com");
app.value("objectValue", { name: "dotnet-tricks.com", totalUsers: 120000 });

Constant - A constant is like as value. The difference between a value and a constant service is that constant
service can be injected into a module configuration function i.e. config() but value service cannot be.

//define module
var app = angular.module('app', []);
//define constant
app.constant("mynumberValue", 200);
app.constant("mystringValue", "webgeekschool.com");
//module configuration function
app.config(function (mynumberValue) { //here value objects can't be injected
 console.log("Before:" + mynumberValue);
 mynumberValue = "New Angular Constant Service";
 console.log("After:" + mynumberValue);
});
answer Nov 11, 2017 by Shivaranjini
...