什么是类型友好注射? [英] What is the type friendly injection?

查看:79
本文介绍了什么是类型友好注射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS文档中,对工厂和服务之间的差异进行了解释. ,值,常量和提供者.

In the AngularJS documentation, there is an explanation of the differences between a factory, a service, a value, a constant and a provider .

最后,我们有一个比较表: 其中一行是类型友好注入".我不明白那是什么.

At the end, we have a comparison table: One of the rows is "type friendly injection". I could not understand what it is.

那是什么意思?另外,为了使值具有此类型友好的注入",这意味着直接使用新的运算符来进行初始化"是什么意思?

What does that mean? Additionally, what does it mean that, in order that a value will have this "type friendly injection", is at the cost of "eager initialization by using new operator directly"?

推荐答案

在AngularJS中,您可以通过多种方式注入依赖项:

In AngularJS, you can inject dependencies in multiple ways:

  • 在指令link中按位置的功能
  • 在指令定义中按名称
  • 在控制器功能中按名称
  • 在工厂功能中按名称
  • 在服务功能中按类型
  • in the directive link function by position
  • in the directive definition by name
  • in the controller function by name
  • in the factory function by name
  • in the service function by type

类型友好注入允许您通过引用隐式调用构造函数:

Type friendly injection allows you to implicity invoke a constructor function by reference:

myApp.service('Pattern', ["Infinity", RegExp]);

,而不是通过使用new关键字来明确声明:

rather than by explicity using the new keyword:

myApp.factory('Pattern', 
 ["Infinity", function(Infinity) 
  {
  return new RegExp(Infinity);
  } 
 ]);

OR

function goInfinity(Infinity)
  {
  return new RegExp(Infinity);
  } 

goInfinity.$inject = ["Infinity"];
myApp.factory('Pattern', goInfinity);

服务"配方产生的服务类似于值"或工厂"配方,但是它是通过使用new运算符调用构造函数来实现的.构造函数可以接受零个或多个参数,这些参数表示此类型的实例所需的依赖项.

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.

急切的初始化意味着constant配方必须返回构造函数才能使用上述语法:

Eager initialization means that a constant recipe must return a constructor in order to use the aforementioned syntax:

function RegExpConstant() 
  {
  return new RegExp(Infinity);
  } 

myApp.constant('Pattern', RegExpConstant)

而不是返回函数,对象或文字值.

rather than returning a function, object, or literal value.

术语来自Java:

服务是一组著名的接口. 服务提供者是服务的特定实现. 工厂是一个对象,它返回对另一个对象的对象引用

A service is a well-known set of interfaces. A service provider is a specific implementation of a service. A factory is an object that returns an object reference to another object

参考

Angular 2的主要目标以及它们的目标将实现

Vojta Jina:依赖注入-NG-Conf

AngularJS:开发人员指南-提供者,服务食谱

AngularJS:不良部分

依赖注入:语法糖在功能组成上

ServiceFinder(JAX-WS RI)

这篇关于什么是类型友好注射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆