天天看點

AngularJS Providers 詳解

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.

你建立的任何 Web 應用都是一些互相依賴的對象組合。這些對象需要被執行個體化并被綁定在一起工作。在 Angular 應用中,這些對象通過注入器服務自動完成執行個體化和綁定。

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

注入器建立了兩種類型的對象,servAngularJS,Providersices(服務)和 specialized objects(特殊對象)。

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

服務等同于對象,它的 API 由編寫服務的開發者定義。

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

特殊對象服從一套專門的 Angular 架構 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 系統資料庫來建立應用所需的對象。

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。你可以這樣去定義它:

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資料綁定來顯示它:

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!

更複雜的例子!

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 這樣重寫:

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 中:

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 能夠建立任何類型的服務,對象常量,方法,甚至一個自定義類型的執行個體。

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(獨角獸)進入太空,這是一個自定義類型的執行個體:

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的依賴:

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:

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 而遭到報應。這就像是給我們某一個後代起名叫“小盆友”。這麼搞會讓老師們困惑。

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 的每次發射配置使用錫箔屏蔽罩,那就再好不過了。我們可做如下配置:

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 的配置方法:

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。我們稱這一階段為應用程式生命周期的運作階段。

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:

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 一樣,是以我們也可以在控制器和模闆中使用它:

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 相同的文法。

We can then use the component like this:

然後,這樣使用元件:

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 先前的例子:

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。

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

no

uses type friendly injection

使用友好的注入方式

yes*

object available in config phase

配置階段可用

yes**

can create functions/primitives

可以建立方法/基元

* 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 示例)。

原文連結: https://docs.angularjs.org/guide/providers

上一篇: 開通