问题描述
在 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);
急切的初始化意味着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:
参考
这篇关于什么是类型友好注射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!