DBMNG数据库管理与应用

独立思考能力,对于从事科学研究或其他任何工作,都是十分必要的。
当前位置:首页 > 经验分享 > Javascript

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.

你创建的任何 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 注册表来创建应用所需的对象。

 

 

变量方式(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。你可以这样去定义它:

var myApp = angular.module('myApp', []); 
myApp.value('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', function DemoController(clientId) { 
this.clientId = clientId; 
}]);<html ng-app="myApp"> 
<body ng-controller="DemoController as demo"> Client ID: {{demo.clientId}} 
</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 更强大的兄弟&mdash;&mdash;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', function clientIdFactory() { 
return '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', function apiTokenFactory(clientId) { 
var encrypt = function(data1, data2) { 
// NSA-proof encryption algorithm: return (data1 + ':' + data2).toUpperCase(); 
}; 
var secret = window.localStorage.getItem('myApp.secret'); 
var apiToken = encrypt(clientId, secret); 
return 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(独角兽)进入太空,这是一个自定义类型的实例:

function UnicornLauncher(apiToken) { 
this.launchedCount = 0; 
this.launch = function() { 
// make a request to the remote api and include the apiToken ... 
this.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", function(apiToken) { 
return new 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.

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

 

供应者方式(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', function UnicornLauncherProvider() { 
var useTinfoilShielding = false; 
this.useTinfoilShielding = function(value) { 
    useTinfoilShielding = !!value; 
}; 
this.$get = ["apiToken", function unicornLauncherFactory(apiToken) { 
// let's assume that the UnicornLauncher constructor was also changed to // accept and use the useTinfoilShielding argument return new UnicornLauncher(apiToken, useTinfoilShielding); 
}]; 
});

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", function(unicornLauncherProvider) { 
  unicornLauncherProvider.useTinfoilShielding(true); 
}]);

 

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', function(unicornLauncherProvider, planetName) { 
  unicornLauncherProvider.useTinfoilShielding(true); 
  unicornLauncherProvider.stampText(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", function DemoController(clientId,planetName) { 
this.clientId = clientId; 
this.planetName = planetName; 
}]);<html ng-app="myApp"> 
<body ng-controller="DemoController as demo"> Client ID: {{demo.clientId}} 
<br> Planet Name: {{demo.planetName}} 
</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 定义的常量和行星的名字,在我们的例子中:&ldquo;行星名称:Greasy Giant&rdquo;(油腻巨人)。

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

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

myApp.directive('myPlanet', ['planetName', function myPlanetDirectiveFactory(planetName) {// directive definition objectreturn {
    restrict: 'E',
    scope: {},
    link: function($scope, $element) { $element.text('Planet: ' + planetName); }
}
}]);

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', function DemoController(clientId) {this.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 示例)。

 

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

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号