At the end,我们有一个比较表:
其中一行是“类型友好注入”。我不明白是什么。
这意味着什么?另外,这意味着,为了使一个值具有这种“类型友好的注入”,是以“直接使用新操作符的急切初始化”为代价的?
>在指令链接功能中按位置
>在指令定义中按名称
>在控制器函数中按名称
>在工厂函数中按名称
>在服务函数中按类型
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)