$provider里factory、service方法
来源:互联网 发布:朱孝远 知乎 编辑:程序博客网 时间:2024/06/10 07:39
总觉得英文文档写的比较直观:
$provide
- - service in module auto
The $provide service has a number of methods for registering components with the $injector. Many of these functions are also exposed on angular.Module
.
An Angular service is a singleton object created by a service factory. These service factories are functions which, in turn, are created by a service provider. The service providers are constructor functions. When instantiated they must contain a property called $get
, which holds the service factory function.
When you request a service, the $injector is responsible for finding the correct service provider, instantiating it and then calling its $get
service factory function to get the instance of the service.
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.
- provider(provider) - registers a service provider with the $injector
- constant(obj) - registers a value/object that can be accessed by providers and services.
- value(obj) - registers a value/object that can only be accessed by services, not providers.
- factory(fn) - registers a service factory function,
fn
, that will be wrapped in a service provider object, whose$get
property will contain the given factory function. - service(class) - registers a constructor function,
class
that will be wrapped in a service provider object, whose$get
property will instantiate a new object using the given constructor function.
See the individual methods for more information and examples.
Methods
provider(name, provider);
Register a provider function with 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 by
Provider
. For example, 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. For example, the $logProvider has a methoddebugEnabled which lets you specify whether the $log service will log debug messages to the console or not.Parameters
Param Type Details namestringThe name of the instance. NOTE: the provider will be available under
providerObjectfunction()name + 'Provider'
key.If the provider is:
Object
: then it should have a$get
method. The$get
method will be invoked using$injector.invoke() when an instance needs to be created.Constructor
: a new instance of the provider will be created using $injector.instantiate(), then treated asobject
.
Returns
Objectregistered provider instance
factory方法直接把一个函数当成是一个对象的$get()方法
factory(name, $getFn);
Register a service 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.Parameters
Param Type Details namestringThe name of the instance.
$getFnfunction()The $getFn for the instance creation. Internally this is a short hand for
$provide.provider(name, {$get: $getFn})
.Returns(可以是任何类型)
Objectregistered provider instance
service(name, constructor);
Register a service constructor, which will be invoked with
new
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.
Parameters
Param Type Details namestringThe name of the instance.
constructorFunctionA class (constructor function) that will be instantiated.
Returns 和factory类似, 但返回的东西必须是对象(数组也是对象)
Objectregistered provider instance
value(name, value);
Register a value service with 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 the value service.Value services are similar to constant services, except that they cannot be injected into a module configuration function (see
angular.Module
) but they can be overridden by an Angular decorator.Parameters
Param Type Details namestringThe name of the instance.
value*The value.
Returns
Objectregistered provider instance
constant(name, value);
Register a constant 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 (see
angular.Module
) and it cannot be overridden by an Angular decorator.Parameters
Param Type Details namestringThe name of the constant.
value*The constant value.
Returns
Objectregistered instance
decorator(name, decorator);
Register a service decorator with 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.
Parameters
Param Type Details namestringThe name of the service to decorate.
decoratorfunction()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:
$delegate
- The original service instance, which can be monkey patched, configured, decorated or delegated to.
- $provider里factory、service方法
- Angular Provider Factory Service
- Angularjs service factory provider
- angularJS 服务--$provide里factory、service方法
- AngularJS Provider/Service/Factory 使用
- angularJs中factory,service,provider
- AngularJS服务(Factory/Provider/Service)
- AngularJS: Factory vs Service vs Provider
- AngularJS之Factory vs Service vs Provider
- AngularJS: Factory vs Service vs Provider
- AngularJS: Factory vs Service vs Provider
- AngularJS 之 Factory vs Service vs Provider
- AngularJS 中的 factory、 service 和 provider区别
- AngularJS 之 Factory vs Service vs Provider
- AngularJS中service,factory,provider的区别
- AngularJS中service,factory,provider的区别
- AngularJS中service,factory,provider的区别
- angularJS provider/factory/service初解析
- ajax原生代码
- Spring Web应用的最大瑕疵
- Google Cloud Messaging (Android 消息推送技术) (一)
- java实现B-Tree
- java中的递归——成员变量,局部变量,静态变量
- $provider里factory、service方法
- 便于游戏开发的工具(自用,持续更新)
- 如何获取手机屏幕大小
- android学习一:tomcat+eclipse 搭建
- R中设置图形参数--函数par()详解
- 为OpenStack私有云创建Linux和windows映像
- 绘制三角形序列的三种方式 :GL_TRIANGLES、GL_TRIANGLE_STRIP和GL_TRIANGLE_FAN
- R一页多图:mfrow用法举例
- 发生错误unrecognized selector sent to instance的原因