[英]AngularJS - module().provider() vs $provide.provider()
Note: I'm not seeking the differences between the value, factory, service, and provider 'recipes', as explained here. 注意:我不是要寻求价值,工厂,服务和提供者“配方”之间的差异, 如此处所述。
I'm looking for clarification on the different ways to define them: Correct me if I'm wrong, but it appears as though 我正在寻找有关定义它们的不同方法的说明:如果我错了,请纠正我,但看起来好像
myApp = angular.module('myApp', [])
.value(...)
.factory(...)
.service(...)
.provider(...)
map to 映射到
$provide.value()
$provide.factory()
$provide.service()
$provide.provider()
And you can use either way. 您可以使用任何一种方式。 I asked this question , and realize I can use
$provide
as a dependency to module().config()
. 我问了这个问题 ,意识到可以将
$provide
用作对module().config()
的依赖。 My questions: 我的问题:
$provide
dependency? $provide
依赖关系? $scope
properties, and don't see $provide
or $injector
. $scope
属性,而没有看到$provide
或$injector
。 Where do these live? The provider methods off the module definition are just short cuts. 模块定义的提供者方法只是捷径。 Use them as often as you like because it leads to shorter, easier to read and understand code.
您可以随意使用它们,因为这样可以使代码更短,更容易阅读和理解。 Less ritual/ceremony is involved than injecting the $provider service and calling that directly.
与注入$ provider服务并直接调用相比,所涉及的仪式/仪式更少。 The main reason to use $provide directly is to access a method on it that is not a short cut from module (such as the decorator) or if you have to do something from within a service or component that is not up at the module definition level.
直接使用$ provide的主要原因是访问它的方法不是模块(例如装饰器)的捷径,或者如果您必须在服务或组件中执行不属于模块定义的方法水平。
The common case for changing a provider after it's definition is when you are integrating a third-party component and want to add or change the behavior. 在定义提供程序之后更改提供程序的常见情况是,当您集成第三方组件并想要添加或更改行为时。 The third-party module will define the service and then you step in and override or extend it in some way that is specific to your app.
第三方模块将定义服务,然后您介入并以特定于您的应用的某种方式覆盖或扩展它。 A common case for example is to take the built-in Angular exception handler and extend that to interface with your own components.
例如,一个常见的情况是采用内置的Angular异常处理程序并将其扩展为与您自己的组件进行接口。
$scope is a special "glue" used for data-binding and only exposes properties/functions that you explicitly set on the $scope. $ scope是用于数据绑定的特殊“胶水”,仅公开在$ scope上显式设置的属性/功能。 All of the other miscellaneous modules/services are stored within Angular's dependency injection container.
所有其他杂项模块/服务都存储在Angular的依赖项注入容器中。 The very first thing Angular does is create an $injector instance to keep track of dependencies.
Angular做的第一件事就是创建一个$ injector实例来跟踪依赖关系。 Therefore $injector === $injector.get('$injector').
因此$ injector === $ injector.get('$ injector')。 Same with $provide.
与$ provide相同。 Anything prefixed with a $ is by convention a service that Angular places in the $injector for you to use.
按照惯例,任何带有$前缀的服务都是Angular放置在$ injector中供您使用的服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.