简体   繁体   English

AngularJS-module()。provider()与$ provide.provider()

[英]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: 我的问题:

  1. When/why would I use angular.module().provider() vs using the $provide dependency? 什么时候/为什么要使用angular.module()。provider()而不是$provide依赖关系?
  2. Is there any way (or reason) to access/change a provider after definition? 定义后是否有任何方式(或理由)访问/更改提供者?
  3. Using AngularJS Batarang for Chrome , I'm looking at the various angular $scope properties, and don't see $provide or $injector . 在Chrome中使用AngularJS Batarang ,我正在查看各种角度$scope属性,而没有看到$provide$injector Where do these live? 这些住在哪里?
  1. 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的主要原因是访问它的方法不是模块(例如装饰器)的捷径,或者如果您必须在服务或组件中执行不属于模块定义的方法水平。

  2. 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异常处理程序并将其扩展为与您自己的组件进行接口。

  3. $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.

相关问题 为什么我无法使用$ injector.get访问使用$ provide.provider创建的服务 - why I can't access a service created using $provide.provider with $injector.get 具有简单模块依赖性的AngularJS未知提供程序 - AngularJS Unknown provider with simple module dependencies AngularJS - 向module.config注入提供程序 - AngularJS - Inject provider to module.config angularJS中的状态提供者和路由提供者 - state provider and route provider in angularJS Angularjs,Typescript,Uglify无法实例化模块myApp…未知提供程序: - Angularjs, Typescript, Uglify Failed to instantiate module myApp … unknown provider: a 将自定义服务注入其他模块时,AngularJS中的未知提供程序 - Unknown Provider in AngularJS when Injecting custom service into different module AngularJS为什么不能使用与另一个模块共享提供程序? - AngularJS why can't I use share a provider with another module? Angularjs通过字符串名称将提供者注入模块工厂函数以进行缩小 - Angularjs inject provider into module factory function by string name for minification 在yeoman中使用动画模块编译AngularJS应用会导致未知的提供程序错误 - Compiling AngularJS app with animation module in yeoman gives unknown provider error AngularJS将工厂注入提供者 - AngularJS injecting a factory into a provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM