加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 什么是类型友好的注射?

发布时间:2020-12-17 08:28:15 所属栏目:安全 来源:网络整理
导读:在 the AngularJS documentation,解释了工厂,服务,值,常量和提供者之间的差异。 At the end,我们有一个比较表: 其中一行是“类型友好注入”。我不明白是什么。 这意味着什么?另外,这意味着,为了使一个值具有这种“类型友好的注入”,是以“直接使用
在 the AngularJS documentation,解释了工厂,服务,值,常量和提供者之间的差异。

At the end,我们有一个比较表:

其中一行是“类型友好注入”。我不明白是什么。

这意味着什么?另外,这意味着,为了使一个值具有这种“类型友好的注入”,是以“直接使用新操作符的急切初始化”为代价的?

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

>在指令链接功能中按位置
>在指令定义中按名称
>在控制器函数中按名称
>在工厂函数中按名称
>在服务函数中按类型

类型友好的注入允许你通过引用调用构造函数:

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

而不是通过使用new关键字的explicity:

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

要么

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

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

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.

Eager初始化意味着一个常数配方必须返回一个构造函数,以便使用上述语法:

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

myApp.constant('Pattern',RegExpConstant)

而不是返回函数,对象或字面值。

命名来自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

参考文献

> Dependency Injection in Angular 2
> The main goals of Angular 2 and how they will be achieved
> Vojta Jina: Dependency Injection – NG-Conf
> AngularJS: Developer Guide – Providers,Service Recipe
> AngularJS: The Bad Parts
> Dependency Injection: Syntax Sugar Over Function Composition
> ServiceFinder (JAX-WS RI)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读