Angularjs supplier ($provide)
$provide
Service responsible for informingAngularJS
How to create a new injectable thing: a service.
Services are defined by something called a provider, and can be used$provide
To create a supplier.
How to create a supplier:
-
Use
$provide
Mediumprovider()
Method to define a supplier; -
By request
$provide
Injected into an applicationconfig
Function to get$provide
Service;
How to define suppliers
-
constant
-
value
-
service
-
factory
-
provider
-
decorator
1. constant
Defining a constant. The value defined by it cannot be changed. It can be injected anywhere, but it cannot be decorated(decorator
Decoration.
DEMO:
HTML Code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div ng-app="myApp">
<div ng-controller="myController">
</div>
</div>
<script>
</script>
<script></script>
</body>
</html>
JS Code:
var myApp = angular.module('myApp', [])
myApp.config(function($provide) {
$provide. Constant ('movietitle ',' Kung Fu Yoga ')
})
myApp.controller('myController', function(movieTitle) {
console.log('movieTitle: ', movieTitle);
})
Grammatical sugar:
Myapp. Constant ('movietitle ',' Kung Fu Yoga ')
2. value
It can bestring
、 number
、 function
, it andconstant
The difference is that it can be modified and cannot be injected intoconfig
But it can bedecorator
Decoration.
HTML Code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div ng-app="myApp">
<div ng-controller="myController">
</div>
</div>
<script>
</script>
<script></script>
</body>
</html>
JS Code:
var myApp = angular.module('myApp', [])
myApp.config(function($provide) {
$provide. Value ('movietitle ',' Kung Fu Yoga ')
})
myApp.controller('myController', function(movieTitle) {
console.log('movieTitle: ', movieTitle);
})
Grammatical sugar:
Myapp. Value ('movietitle ',' Kung Fu Yoga ')
3. service
It’s an injectable constructor, andAngularJS
It’s singleton. Use it toController
China Communications or data sharing is suitable
var myApp = angular.module('myApp', [])
myApp.config(function($provide) {
$provide.service('movie', function() {
This. Title = Kung Fu Yoga
})
})
myApp.controller('myController', function(movie) {
console.log('movieTitle: ', movie.title);
})
Grammatical sugar:
myApp.service('movie', function () {
This. Title = Kung Fu Yoga
})
Be careful:
stayservice
There’s no need to return anything inside, becauseAngularJS
Would callnew
Keyword to create an object. But returning a custom object doesn’t seem to be wrong.
4. factory
It’s an injectablefunction
, it andservice
The difference is:factory
It’s ordinary.function
Andservice
It’s a constructor(constructor
) soAngularJS
In callservice
Use timenew
Keywords, while callingfactory
Call normalfunction
Sofactory
Can return anything, andservice
Can not return (check the function of new keyword)
var myApp = angular.module('myApp', [])
myApp.config(function($provide) {
$provide.factory('movie', function() {
return {
Title: 'Kung Fu Yoga'
}
})
})
myApp.controller('myController', function(movie) {
console.log('movieTitle: ', movie.title);
})
Grammatical sugar:
$provide.factory('movie', function() {
return {
Title: 'Kung Fu Yoga'
}
})
Be careful:
factory
It can return anything. It’s actually a$get
Methodprovider
5. provider
provider
It’s their boss. The top one is almost (exceptconstant
Both areprovider
Encapsulation,provider
There must be one$get
Method, of courseprovider
Is a configurablefactory
。
JS Code:
var myApp = angular.module('myApp', [])
myApp.provider('movie', function() {
var version
return {
setVersion: function(value) {
version = value
},
$get: function() {
return {
Title: 'Kung Fu Yoga' + version
}
}
}
})
myApp.config(function(movieProvider) {
Movieprovider. Setversion ('playing hot ')
})
myApp.controller('myController', function(movie) {
console.log('movieTitle: ', movie.title);
})
Be careful:
-
Here
config
Method injectionmovieProvider
,config
Only suppliers can be injected into the method (two exceptions are$provide
and$injector
)In the name of humpmovieProvider
,AngularJS
It will automatically inject you into its suppliers. -
movie
It’s a supplier
6.decorator
decorator
Noprovider
, it is used to decorate otherprovider
Yes, it can’t be decoratedconstant
(becauseconstant
Not throughprovider()
Method).
JS Code:
var myApp = angular.module('myApp', [])
//Myapp. Value ('movietitle ',' Kung Fu Yoga 6 ')
myApp.config(function($provide) {
$provide. Value ('movietitle ',' Kung Fu Yoga 7 ')
$provide.decorator('movieTitle', function($delegate) {
Return $delegate + '- Test'
})
})
myApp.controller('myController', function(movieTitle) {
console.log('movieTitle: ', movieTitle);
})
summary
-
All suppliers are instantiated only once, that is, they are all singleton
-
except
constant
, all suppliers can be decorated(decorator
Decoration -
value
It’s a simple injectable value -
service
It’s an injectable constructor -
factory
Is an injectable method -
decorator
Other suppliers can be modified or encapsulated (exceptconstant
) -
provider
Is a configurablefactory