AngularJS Providers 详解

供应者(Providersimage

Each web application you build is composed of objects that collaborate to get stuff done. These objects need to be instantiated and wired together for the app to work. In Angular apps most of these objects are instantiated and wired together automatically by the injector service.html

你建立的任何 Web 应用都是一些互相依赖的对象组合。这些对象须要被实例化并被绑定在一块儿工做。在 Angular 应用中,这些对象经过注入器服务自动完成实例化和绑定。ios

The injector creates two types of objects, services and specialized objects.angularjs

注入器建立了两种类型的对象,services(服务)和 specialized objects(特殊对象)。web

Services are objects whose API is defined by the developer writing the service.spring

服务等同于对象,它的 API 由编写服务的开发者定义。bootstrap

Specialized objects conform to a specific Angular framework API. These objects are one of controllers, directives, filters or animations.设计模式

特殊对象服从一套专门的 Angular 框架 API。这些对象是控制器、指令、过滤器或动画效果中的一个。api

The injector needs to know how to create these objects. You tell it by registering a "recipe" for creating your object with the injector. There are five recipe types.浏览器

注入器须要知晓如何去建立这些对象。你经过注册一个“recipe(配方)” 来告诉注入器去建立你的对象。共有五种类型的配方。缓存

The most verbose, but also the most comprehensive one is a Provider recipe. The remaining four recipe types — Value, Factory, Service and Constant — are just syntactic sugar on top of a Provider recipe.

最繁琐,也是功能最全面的是 Provider recipe。剩下的四种类型——Value,Factory,Service 和 Constant —— 仅仅是 Provider recipe 的语法糖。

Let's take a look at the different scenarios for creating and using services via various recipe types. We'll start with the simplest case possible where various places in your code need a shared string and we'll accomplish this via Value recipe.

接下来,咱们看看如何在不一样场景下经过不一样的 recipe types 建立和使用 services 。咱们将从最简单的例子开始,经过 Value recipe在代码中共享一个字符串。

 

 

Note: A Word on Modules

注意:模块概要

 

In order for the injector to know how to create and wire together all of these objects, it needs a registry of "recipes". Each recipe has an identifier of the object and the description of how to create this object.

为了让注入器知晓如何建立和绑定全部的对象,它须要一个"recipes"的注册表。每一个 recipe 都有惟一的对象标识符和以及何建立这个对象的描述。

Each recipe belongs to an Angular module. An Angular module is a bag that holds one or more recipes. And since manually keeping track of module dependencies is no fun, a module can contain information about dependencies on other modules as well.

每一个 recipe 属于一个 Angular 模块。Angular 模块是一个保存了一个或多个 recipes 的袋子。手动跟踪模块依赖关系显然不是一个好方法,所以一个模块也能够包含依赖其它模块的相关信息。

When an Angular application starts with a given application module, Angular creates a new instance of injector, which in turn creates a registry of recipes as a union of all recipes defined in the core "ng" module, application module and its dependencies. The injector then consults the recipe registry when it needs to create an object for your application.

一个 Angular 应用开始于一个给定的应用模块时,Angular 会建立一个新的注入器实例,进而按照全部核心"ng"模块、应用模块和在它的依赖中统必定义的 recipes 来建立一个 recipes 的注册表。而后,注入器经过查询 recipes 注册表来建立应用所需的对象。

 

 

变量方式(Value Recipe

Let's say that we want to have a very simple service called "clientId" that provides a string representing an authentication id used for some remote API. You would define it like this:

假定咱们想要得到一个很是简单的 service 叫作"clientId",它提供一个字符串用于表示某些远程 API 的认证 id。你能够这样去定义它:

 myApp = angular.module('myApp''clientId', 'a12345654321x');

Notice how we created an Angular module called myApp, and specified that this module definition contains a "recipe" for constructing the clientId service, which is a simple string in this case.

注意,咱们建立一个名为 myApp 的 Angular 模块,而后指定了一个包含构建 clientId service 的配方,这只是一个字符串的简单例子。

And this is how you would display it via Angular's data-binding:

如下是如何经过 Angular数据绑定来显示它:

myApp.controller('DemoController', ['clientId', .clientId =<html ng-app="myApp"> 
<body ng-controller="DemoController as demo"></body> 
</html>

In this example, we've used the Value recipe to define the value to provide when DemoController asks for the service with id "clientId".

在这个例子中,咱们使用了 Value recipe 去定义这个 value,提供给 DemoController 请求这个服务的 id "clientId"。

On to more complex examples!

更复杂的例子!

 

 

工厂方式(Factory Recipe

The Value recipe is very simple to write, but lacks some important features we often need when creating services. Let's now look at the Value recipe's more powerful sibling, the Factory. The Factory recipe adds the following abilities:

这个 Value recipe 写起来很是简单,但缺少建立 service时常常用到的一些重要特征。如今让咱们看看 Value recipe 更强大的兄弟——Factory。Factory recipe 增长了如下能力:

ability to use other services (have dependencies)

可以使用其它的 services(依赖)

service initialization

service 初始化

delayed/lazy initialization

延迟/懒惰初始化

The Factory recipe constructs a new service using a function with zero or more arguments (these are dependencies on other services). The return value of this function is the service instance created by this recipe.

Factory recipe经过一个包含有零个或多个参数(它所依赖的其它 services)的方法构造一个新的 service。这个方法的返回值是由 recipe 建立的这个服务的实例。

Note: All services in Angular are singletons. That means that the injector uses each recipe at most once to create the object. The injector then caches the reference for all future needs.

注意:Angular 中全部的服务都是单例模式。这意味着注入器建立这个对象时,仅使用一次recipe。而后注入器缓存全部未来须要的引用。

Since Factory is more powerful version of the Value recipe, you can construct the same service with it. Using our previous clientId Value recipe example, we can rewrite it as a Factory recipe like this:

由于 Factory 是Value recipe 更强大的版本,你能够构造和它同样的服务。使用咱们以前的 clientId Value recipe 的例子,能够采用 Factory recipe 这样重写:

myApp.factory('clientId',  'a12345654321x'

But given that the token is just a string literal, sticking with the Value recipe is still more appropriate as it makes the code easier to follow.

但考虑到令牌仅仅是一个字符串常量,使用 Value recipe 更恰当,也更易于代码的阅读。

Let's say, however, that we would also like to create a service that computes a token used for authentication against a remote API. This token will be called apiToken and will be computed based on the clientId value and a secret stored in the browser's local storage:

好比说,咱们想建立一个用于计算远程 API 认证令牌的服务。这个令牌将被称作 apiToken,并计算基于 clientId 的值,而后加密存储于浏览器 Local Storage 中:

myApp.factory('apiToken', ['clientId',  encrypt =  (data1 + ':' + secret = window.localStorage.getItem('myApp.secret' apiToken =

In the code above, we see how the apiToken service is defined via the Factory recipe that depends on clientId service. The factory service then uses NSA-proof encryption to produce an authentication token.

在上面的代码中,咱们看到了如何经过工厂方法定义这个依赖于 clientId 服务的 apiToken 服务。这个工厂服务使用 NSA-proof 加密去产生一个认证令牌。

Note: It is best practice to name the factory functions as <serviceId>Factory (e.g. apiTokenFactory). While this naming convention is not required, it helps when navigating the code base or looking at stack traces in the debugger.

注意:工厂方法命名的最佳实践相似于<serviceId>Factory (e.g. apiTokenFactory)。虽然这种命名习惯不是必须的,但它有助于代码库导航或查看调试器的堆栈跟踪。

Just like with Value recipe, Factory recipe can create a service of any type, whether it be a primitive, object literal, function, or even an instance of a custom type.

与 Value recipe 同样,Factory recipe 可以建立任何类型的服务,对象常量,方法,甚至一个自定义类型的实例。

 

 

 

服务方式(Service Recipe

JavaScript developers often use custom types to write object-oriented code. Let's explore how we could launch a unicorn into space via our unicornLauncher service which is an instance of a custom type:

JavaScript 开发者经常使用自定义的类型去编写面向对象的代码。让咱们探究如何经过 unicornLauncher 服务发射一个 unicorn(独角兽)进入太空,这是一个自定义类型的实例:

.launchedCount = 0.launch = .launchedCount++

We are now ready to launch unicorns, but notice that UnicornLauncher depends on our apiToken. We can satisfy this dependency on apiToken using the Factory recipe:

咱们如今来准备发射 unicorn,但请注意 UnicornLauncher 依赖了咱们的 apiToken。咱们可使用 Factory recipe 来知足这个 apiToken的依赖:

myApp.factory('unicornLauncher', ["apiToken",

This is, however, exactly the use-case that Service recipe is the most suitable for.

就是这样。不过,使用 Service recipe 才是最为恰当的例子。

The Service recipe produces a service just like the Value or Factory recipes, but it does so by invoking a constructor with the new operator. The constructor can take zero or more arguments, which represent dependencies needed by the instance of this type.

这个 Service recipe 产生一个相似于 Value 和 Factory recipes ,但它经过调用构造函数去执行 new 操做。这个构造函数能够接受零或多个参数,表示这个类型实例所需的依赖项。

Note: Service recipes follow a design pattern called constructor injection.

注意:Service recipes 的设计模式被称之为构造函数注入。

Since we already have a constructor for our UnicornLauncher type, we can replace the Factory recipe above with a Service recipe like this:

由于咱们已经有了一个 UnicornLauncher 类型的构造函数,就可以像这样去用 Service recipe 替换 Factory recipe:

myApp.service('unicornLauncher', ["apiToken", UnicornLauncher]);

Much simpler!

多么简单!

Note: Yes, we have called one of our service recipes 'Service'. We regret this and know that we'll be somehow punished for our mis-deed. It's like we named one of our offspring 'Child'. Boy, that would mess with the teachers.

注意:是的,咱们有一个被称为“Service”的 service recipes。很遗憾咱们将由于 mis-deed 而遭到报应。这就像是给咱们某一个后代起名叫“小盆友”。这么搞会让老师们困惑。

 

供应者方式(Provider Recipe

There are two more recipe types left to cover. They are both fairly specialized and are used infrequently. As already mentioned in the intro, the Provider recipe is the core recipe type and all the other recipe types are just syntactic sugar on top of it. It is the most verbose recipe with the most abilities, but for most services it's overkill.

还有两个 recipe 类型。它们都至关特殊,不多使用。如前述,Provider recipe 是核心 recipe 类型,全部其余的配方类型只是基于它的语法糖。它是最详细功能最多的 recipe,但对于大多数服务来讲,它是多余的。

Provider recipe is syntactically defined as a custom type that implements a $get method. This method is a factory function just like the one we use in Factory recipe. In fact, if you define a Factory recipe, an empty Provider type with the $get method set to your factory function is automatically created under the hood.

Provider recipe 是语法定义为一个自定义类型,实现 $get 的方法。这个方法是一个工厂方法,就像咱们在 Factory recipe 中使用的同样。事实上,若是你定义一个 Factory recipe,钩子会自动建立一个包含空 Provider 类型 $get 方法的工厂方法。

You should use the Provider recipe only when you want to expose an API for application-wide configuration that must be made before the application starts. This is usually interesting only for reusable services whose behavior might need to vary slightly between applications.

只有当你但愿一个应用程序配置的 API 必须在应用程序启动以前被建立,你才应该使用 Provider recipe 。一般只关注可重用服务的行为可能在应用程序间略有不一样。

Let's say that our unicornLauncher service is so awesome that many apps use it. By default the launcher shoots unicorns into space without any protective shielding. But on some planets the atmosphere is so thick that we must wrap every unicorn in tinfoil before sending it on its intergalactic trip, otherwise they would burn while passing through the atmosphere. It would then be great if we could configure the launcher to use the tinfoil shielding for each launch in apps that need it. We can make it configurable like so:

咱们假设 unicornLauncher是个很不错的服务,许多应用程序都在使用它。默认状况下,launcher 发射 unicorns 进入太空时没有任何防御屏蔽。但某些行星的大气层实在是太厚了,以致于咱们在星际旅行发射以前,必须用锡纸包住每个 unicorns,不然它们将会在穿越大气层时烧毁。若是咱们能够按照应用程序的须要去为 launcher 的每次发射配置使用锡箔屏蔽罩,那就再好不过了。咱们可作以下配置:

myApp.provider('unicornLauncher',  useTinfoilShielding = .useTinfoilShielding = = !!.$get = ["apiToken",

To turn the tinfoil shielding on in our app, we need to create a config function via the module API and have theUnicornLauncherProvider injected into it:

为了在咱们的应用程序中启用锡纸屏蔽罩,咱们须要经过模块的 API 建立一个含有注入 UnicornLauncherProvider 的配置方法:

myApp.config(["unicornLauncherProvider",

 

Notice that the unicorn provider is injected into the config function. This injection is done by a provider injector which is different from the regular instance injector, in that it instantiates and wires (injects) all provider instances only.

请注意 unicorn provider 被注入的配置方法,这种注入是经过 provider 的注入器完成的,不一样于普通的实例注入器只是由 provider 实例进行实例化和绑定(注入)。

During application bootstrap, before Angular goes off creating all services, it configures and instantiates all providers. We call this the configuration phase of the application life-cycle. During this phase services aren't accessible because they haven't been created yet.

在应用程序启动期间,Angular 建立的全部服务前,配置和实例化全部的 providers。咱们称之为应用程序生命周期中的配置阶段。在此阶段服务还不可用,由于它们尚未被建立。

Once the configuration phase is over, interaction with providers is disallowed and the process of creating services starts. We call this part of the application life-cycle the run phase.

配置阶段结束,providers 交互被禁止,开始建立 services。咱们称这一阶段为应用程序生命周期的运行阶段。

 

 

 

常量方式(Constant Recipe

We've just learned how Angular splits the life-cycle into configuration phase and run phase and how you can provide configuration to your application via the config function. Since the config function runs in the configuration phase when no services are available, it doesn't have access even to simple value objects created via Value recipe.    
咱们已经学会了如何区分应用程序生命周期中的配置阶段和运行阶段,如何经过配置方法向您的应用程序提供配置。因为配置方法运行在配置阶段,此时尚无服务可用,所以它不能访问任何对象,哪怕是经过 Value recipe 建立的 value 对象。

Since simple values, like url prefix, don't have dependencies or configuration, it is often handy to make them available in both the configuration and run phases. This is what the Constant recipe is for.

而简单的值,好比 url 的前缀,没有依赖或配置,须要在配置和运行阶段皆可以使用。这就是 Constant recipe。

Let's say that our unicornLauncher service can stamp a unicorn with the planet name it's being launched from if this name was provided during the configuration phase. The planet name is application specific and is used also by various controllers during the runtime of the application. We can then define the planet name as a constant like this:

假设 unicornLauncher 服务能够标出 unicorn 发射自哪颗行星,并且这个行星的名字须要在配置阶段提供。同时,星球的名字会由应用程序指定,而且被多个控制器在运行阶段使用。

We can then define the planet name as a constant like this:

咱们能够按照以下方式定义这个星球的名字为一个常量:

myApp.constant('planetName', 'Greasy Giant');

We could then configure the unicornLauncherProvider like this:

咱们这样能够配置 unicornLauncherProvider:

myApp.config(['unicornLauncherProvider', 'planetName',

And since Constant recipe makes the value also available at runtime just like the Value recipe, we can also use it in our controller and template:

因为在运行阶段,Constant recipe 建立的值和 value recipe 同样,因此咱们也能够在控制器和模板中使用它:

myApp.controller('DemoController', ["clientId", "planetName", .clientId =.planetName =<html ng-app="myApp"> 
<body ng-controller="DemoController as demo"><br></body> 
</html>

 

 

特殊目的对象(Special Purpose Objects

Earlier we mentioned that we also have special purpose objects that are different from services. These objects extend the framework as plugins and therefore must implement interfaces specified by Angular. These interfaces are Controller, Directive, Filter and Animation.

如上所述,我还有不一样于 services,用于特殊目的对象。这些扩展做为框架的插件,所以必须实现 Angular 指定的接口。这些接口是:控制器、指令、过滤器和动画效果。

The instructions for the injector to create these special objects (with the exception of the Controller objects) use the Factory recipe behind the scenes.

举例说明注入器建立特殊对象(控制器对象除外)使用 Factory recipe。

Let's take a look at how we would create a very simple component via the directive api that depends on the planetNameconstant we've just defined and displays the planet name, in our case: "Planet Name: Greasy Giant".

让咱们看一下如何经过指令 api 建立一个很是简单的组件,取决于咱们刚才 planetName 定义的常量和行星的名字,在咱们的例子中:“行星名称:Greasy Giant”(油腻巨人)。

Since the directives are registered via Factory recipe, we can use the same syntax as with factories.

自从经过工厂方法注册指令以后,咱们就可使用与 factory 相同的语法。

myApp.directive('myPlanet', ['planetName', 'E'($scope, $element) { $element.text('Planet: ' +

We can then use the component like this:

而后,这样使用组件:

<html ng-app="myApp">
<body>
<my-planet></my-planet>
</body>
</html>

Using Factory recipes you can also define Angular's filters and animations, but the controllers are a bit special. You create a controller as a custom type that declares its dependencies as arguments for its constructor function. This constructor is then registered with a module. Let's take a look at the DemoController, created in one of the early examples:

使用 Factory recipes,你还能够定义 Angular 的过滤器和动画效果,可是控制器有些许特殊。建立一个控制器做为自定义类型,声明包含做为其依赖项参数的构造函数。而后注册这个构造函数到一个模块。让咱们看看 DemoController 先前的例子:

myApp.controller('DemoController', ['clientId', .clientId =

The DemoController is instantiated via its constructor every time the app needs an instance of DemoController (in our simple app it's just once). So unlike services, controllers are not singletons. The constructor is called with all the requested services, in our case the clientId service.

DemoController 是根据应用程序的须要,经过其构造函数实例化的(在咱们的简单应用中只有一次)。与服务不一样,控制器并非单例的。构造函数被全部请求的服务调用,在咱们的案例中是 clientId service。

 

 

 

结论(Conclusion

To wrap it up, let's summarize the most important points:

归纳上述内容,让我来总结一下最重要的几点:

The injector uses recipes to create two types of objects: services and special purpose objects.

注入器经过使用 recipes 来建立两种类型的对象:服务和特殊目的对象。

There are five recipe types that define how to create objects: Value, Factory, Service, Provider and Constant.

一共有五种类型的 recipe 用于定义如何建立对象:变量、工厂、服务、供应者和常量。

Factory and Service are the most commonly used recipes. The only difference between them is that Service recipe works better for objects of custom type, while Factory can produce JavaScript primitives and functions.

工厂和服务是最经常使用的方式。二者仅有的不一样是服务方式对于自定义类型对象效果更好,而工厂方式能够提供 JavaScript 基元和方法。

The Provider recipe is the core recipe type and all the other ones are just syntactic sugar on it.

供应者方式是核心方式,全部其它方式都是它的语法糖。

Provider is the most complex recipe type. You don't need it unless you are building a reusable piece of code that needs global configuration.

供应者是最复杂的方式类型。除非你正在构建一段须要全局配置的可重用代码,不然不要使用它。

All special purpose objects except for Controller are defined via Factory recipes.

全部特殊目的对象都经过工厂方式来定义,除了控制器。

Features / Recipe type

Factory

Service

Value

Constant

Provider

can have dependencies              
支持依赖注入

yes

yes

no

no

yes

uses type friendly injection            
使用友好的注入方式

no

yes

yes*

yes*

no

object available in config phase            
配置阶段可用

no

no

no

yes

yes**

can create functions/primitives              
能够建立方法/基元

yes

no

yes

yes

yes

* at the cost of eager initialization by using new operator directly

以使用 new 操做符初始化为代价。

** the service object is not available during the config phase, but the provider instance is (see the unicornLauncherProviderexample above).

** 服务对象在配置阶段不可用,除了供应者实例(参见上面的 unicornLauncherProvider 示例)。

目前已有部分产品支持AngularJS,Wijmo 就是其中之一。它是为企业应用程序开发而推出的一系列包含HTML5和JavaScript的开发控件集,不管应用程序是移动端、PC端、仍是必需要支持IE6,Wijmo 均能知足需求。

原文连接: https://docs.angularjs.org/guide/providers