Difference between constant and value in AngularJS

What is the difference between constant and value? we can change both values.

var app = angular.module(‘myApp’,[]);
app.constant(‘appName’,‘My App’);
app.value(‘applicationName’,‘Sample’);
+4
source share
3 answers

Constants can fit anywhere, and values ​​cannot be added anywhere. Also, constants cannot be intercepted by decorators, while values ​​can be intercepted by decorators.

Also see: Value and Constants

The difference between value and permanent service is that the former can only be entered (and therefore used) in the service or while the latter can also be entered into the module (I will talk about the configuration of the module function in a future post).

+1
source

1) 2)

1) 2) , , factory, config.

var app = angular.module('app', []);

app.value('greeting', 'Hello');

app.config(function(greeting){
    var radius = 4;
    //PI can be injected here in the config block
    var perimeter = 2 * PI * radius;
});

, .

+2

:

, . - , , - , .

Values ​​are great for pieces of data that can and will change. As shown above, user data or something else where you just want to keep a reference to a changing value stored globally without creating a messy global variable.

full article: Constants and Values: Global Variables in AngularJS The Right Way

0
source

Source: https://habr.com/ru/post/1610270/


All Articles