When we talk about Providers, are usually referring Dependency Providers. Related to the DI token, and used with the Dependency Injection.
Providers are necessary for Injectors, otherwise, the Injectors won't know how to create the dependency.
Provider is an object with two properties
provide property is a token key for dependency value, and configur the injector
a provider definition key, could be useClass, useValue, useExisting, and useFactory.
The Provider object literal
Alternative class providers
The following code tells the injector to return a BetterLogger instance when the component asks for a logger using the Logger token.
[{ provide: Logger, useClass: BetterLogger }]
Class providers with dependencies
Aliased class providers
Value providers
It's easier to provide a ready-made object rather than ask the injector to create it from a class.
To inject an object that is already created, here is the situation for the useValue .
Non-class dependencies
Injection for string,function or object.
Factory providers
Rules for Providers
When a Provider is inside app.module.ts, then it is availabe, and seen for everyone
When a Provider is inside component, `.component.ts`**, then it is only available and seen for this component, and its children.
When the Provider both from the local component and root module have the same token, then the one in the local component will override the content in root module.
app.module.ts
@NgModule ({
providers: [ ProductService ]
})
is equal to
providers: [ provide: ProductService, useClass:ProductService ]
providers is an Array , and each element inside is a provider, a provider defines how to instance an object before injected into any component.
@Injector will find the token of Service inside the construtor, then will find the provide:ProductService with the tool provided by Angular, then use the useClass to new a ProductService, here the ProductService could be differently, e.g. AnotherProductService.
When we talk about
Providers
, are usually referringDependency Providers
. Related to theDI token
, and used with theDependency Injection
.Providers are necessary for Injectors, otherwise, the Injectors won't know how to create the dependency.
Provider is an
object
with two propertiesuseClass
,useValue
,useExisting
, anduseFactory
.The Provider object literal
Alternative class providers
The following code tells the
injector
to return aBetterLogger instance
when the component asks for a logger using theLogger
token.Class providers with dependencies
Aliased class providers
Value providers
It's easier to provide a ready-made object rather than ask the injector to create it from a class.
To inject an object that is already created
, here is the situation for theuseValue
.Non-class dependencies
Injection for string,function or object.
Factory providers
Rules for Providers
Provider
is insideapp.module.ts
, then it is availabe, and seen for everyoneProvider
is inside component, `.component.ts`**, then it is only available and seen for this component, and its children.Provider
both from the local component and root module have the same token, then the one in the local component willoverride
the content in root module.providers is an
Array
, and each element inside is aprovider
, a provider defines how to instance an object before injected into any component.@Injector
will find the token of Service inside theconstrutor
, then will find theprovide:ProductService
with the tool provided by Angular, then use theuseClass
to new a ProductService, here the ProductService could be differently, e.g. AnotherProductService.Three levels for injecting Providers
providedIn
providers
providers