It is also important for ANGULARJS to create services through constant () and value (). Their similarities are: All can accept two parameters, name and value. Difference: constant () can register an existing variable value as a service and inject it into other parts of the app. Where name is the name of the registered constant, value or object that is a registered constant. Example: Value: Angular.module (' myApp '). Constant (' ApiKey ', ' 123123123 ')
. Controller (' Mycontroller ', function ($scope, ApiKey) {
You can use Apikey as a constant as above
Use 123123123 as the value of the string
$scope. ApiKey = ApiKey;
}); value is an object: Angular.module (' myApp '). Constant (' ApiKey ', {name:[],age:[],date:[]})
. Factory (' MyFactory ', function (ApiKey, $scope) {
Apikey.name = "Lyy";
}), the name of value () is also the service name that needs to be registered, and value returns the value as an instance that can be injected. Ngular.module (' myApp ')
. Value (' ApiKey ', ' 123123123 '); The biggest difference is that constants can be injected into the configuration function, but not the value.
Typically, you can use value () to register a service object or function and configure the data with constant ().
Angular.module (' myApp ', [])
. Constant (' ApiKey ', ' 123123123 ')
. config (function (apiKey) {
Here Apikey will be assigned a value of 123123123
Just like the set above.
})
. Value (' Fbid ', ' 231231231 ')
. config (function (fbid) {
This will throw an error, unknown to the Provider:fbid
Because this value cannot be accessed inside the config function
});
AngularJS constant and value