AngularJS constant and value

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.