Showing posts with label constant. Show all posts
Showing posts with label constant. Show all posts

Tuesday, 24 May 2016

Difference between constant and value

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.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 configurationsbut 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!';
    });
});

Thursday, 21 April 2016

Constant in angular:


A constant can be injected everywhere. A constant can not be intercepted by a decorator, that means that the value of a constant should never be changed (though it is still possible to change it programmatically in Angular 1.x).

Syntax:
.constant(name, value);

name --> name of constant
value --> value of constant

Example:

var myAppangular.module('myApp', []);

myApp.constant("clientId", "123456");

myApp.config(['clientId', function(clientId){
console.log(clientId);
}])

myApp.controller('myController',['clientId', function(clientId){
console.log(clientId);
}])

so, the difference between constant and value is constant inject in  config block but value can't