简体   繁体   English

$ injector.get()的副作用? AngularJS

[英]Side effect of $injector.get()? AngularJS

I'm trying to decorate $log with a service that depends on $http and it's raising a circular dependency error. 我正在尝试使用依赖$ http的服务来装饰$ log,并引发循环依赖错误。 Some fixes point to get my factory through $injector.get('myService'), so DI of myService is delayed and doesn't provoke the error. 一些修补程序指出要通过$ injector.get('myService')来获取我的工厂,因此myService的DI会延迟并且不会引发错误。

Anyways, whether it's delayed or not, there's a circular dependency and I'm trying to guess what can go wrong doing this, as I though $injector.get is an anti-pattern. 无论如何,无论延迟与否,都有一个循环依赖关系,我试图猜测这样做有什么问题,尽管$ injector.get是一个反模式。

So, what side effect / bug can produce avoiding circular dependency injection using $inject.get()? 那么,使用$ inject.get()可以避免循环依赖注入的副作用/错误是什么呢?

In my previous project we tried to implement the same but faced with the circular dependency but we came up with a different solution in order to handle this error. 在我的上一个项目中,我们尝试实现相同的功能,但是面临循环依赖,但是我们提出了不同的解决方案来处理此错误。

1.what side effect / bug can produce avoiding circular dependency injection using $inject.get()? 1,使用$ inject.get()有什么副作用/错误可以避免循环依赖注入?

while coming to the circular dependency is always the sign of mixing of concerns . 而进入循环依赖始终是担忧混杂的标志。 There is a better example explaining this phenomenon by Miško Hevery on his awesome blog. MiškoHevery在其超赞的博客上有一个更好的例子来解释这种现象。

In short for answering your this (side effect / bug can produce) part of question : you probably will have a third service hidden somewhere, which is the only part of your code really needed by the two others , so if any error occurred in that hidden service, it may not provoke error , this might be the side effect you probably would be facing . 简而言之,就是回答您的问题(可能产生副作用/错误)的一部分:您可能会在某处隐藏了第三项服务,这是另外两个人真正需要的代码的唯一部分,因此如果发生任何错误,隐藏服务,它可能不会引发错误,这可能是您可能面临的副作用。

To overcome this unhandled behaviour and If you are willing to try another solution by adding Jquery and logging the error using Ajax , i can give you the angular module which we implemented for logging error without causing circular dependency . 为了克服这种未处理的行为,如果您愿意尝试通过添加Jquery并使用Ajax记录错误来尝试另一种解决方案,我可以为您提供我们为记录错误而实现的angular模块,而不会引起循环依赖性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM