©
本文檔使用php中文網手册發布
The $provide service has a number of methods for registering components with the $injector. Many of these functions are also exposed onangular.Module
.
An AngularServiceis a singleton object created by aservice factory. Theseservice factoriesare functions which, in turn, are created by aservice provider. Theservice providersare constructor functions. When instantiated they must contain a property called$get
, which holds theservice factoryfunction.
When you request a service, the $injector is responsible for finding the correctservice provider, instantiating it and then calling its$get
service factoryfunction to get the instance of theService.
Often services have no configuration options and there is no need to add methods to the service provider. The provider will be no more than a constructor function with a$get
property. For these cases the $provide service has additional helper methods to register services without specifying a provider.
fn
, that will be wrapped in aservice providerobject, whose$get
property will contain the given factory function.class
that will be wrapped in aservice providerobject, whose$get
property will instantiate a new object using the given constructor function.See the individual methods for more information and examples.
Provider(名称,provider);
Register aprovider functionwith the $injector. Provider functions are constructor functions, whose instances are responsible for "providing" a factory for a service.
Service provider names start with the name of the service they provide followed byProvider
. 例如,the $log service has a provider called $logProvider.
Service provider objects can have additional methods which allow configuration of the provider and its service. Importantly, you can configure what kind of service is created by the$get
method, or how that service will act. 例如,the $logProvider has a method debugEnabled which lets you specify whether the $log service will log debug messages to the console or not.
参数 | 类型 | 详述 |
---|---|---|
name | string | The name of the instance. 注意: the provider will be available under |
provider | Objectfunction() | If the provider is:
|
Object | registered provider instance |
factory(名称,$getFn);
Register aservice factory, which will be called to return the service instance. This is short for registering a service where its provider consists of only a$get
property, which is the given service factory function. You should use $provide.factory(getFn) if you do not need to configure your service in a provider.
参数 | 类型 | 详述 |
---|---|---|
name | string | The name of the instance. |
$getFn | function() | The $getFn for the instance creation. Internally this is a short hand for |
Object | registered provider instance |
Service(名称,constructor);
Register aservice constructor, which will be invoked withnew
to create the service instance. This is short for registering a service where its provider's$get
property is the service constructor function that will be used to instantiate the service instance.
You should use $provide.service(class) if you define your service as a type/class.
参数 | 类型 | 详述 |
---|---|---|
name | string | The name of the instance. |
constructor | Function | A class (constructor function) that will be instantiated. |
Object | registered provider instance |
value(名称,value);
Register avalue servicewith the $injector, such as a string, a number, an array, an object or a function. This is short for registering a service where its provider's$get
property is a factory function that takes no arguments and returns thevalue service.
Value services are similar to constant services, except that they cannot be injected into a module configuration function (seeconfig
) but they can be overridden by an Angular decorator.
参数 | 类型 | 详述 |
---|---|---|
name | string | The name of the instance. |
value | * | The value. |
Object | registered provider instance |
constant(名称,value);
Register aconstant service, such as a string, a number, an array, an object or a function, with the $injector. Unlike value it can be injected into a module configuration function (seeconfig
) and it cannot be overridden by an Angular decorator.
参数 | 类型 | 详述 |
---|---|---|
name | string | The name of the constant. |
value | * | The constant value. |
Object | registered instance |
decorator(名称,decorator);
Register aservice decoratorwith the $injector. A service decorator intercepts the creation of a service, allowing it to override or modify the behaviour of the service. The object returned by the decorator may be the original service, or a new service object which replaces or wraps and delegates to the original service.
参数 | 类型 | 详述 |
---|---|---|
name | string | The name of the service to decorate. |
decorator | function() | This function will be invoked when the service needs to be instantiated and should return the decorated service instance. The function is called using the injector.invoke method and is therefore fully injectable. Local injection arguments:
|