AngularJS中的$ injector和Inject()之间有什么区别? [英] Any difference between $injector and Inject() in AngularJS?

查看:373
本文介绍了AngularJS中的$ injector和Inject()之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间是否有任何区别/偏好:

Is there any difference/preference between:

inject(function($injector) {
    rootScope = $injector.get('$rootScope');
});

还有

inject(function($rootScope){
    rootScope = $rootScope;
});

在Jasmine中将资源注入测试中是否等于等价物?

Are the eqal as far as getting a resource injected into a test in Jasmine?

推荐答案

文档注入功能:

inject函数将一个函数包装为一个可注入函数.这 inject()为每个测试创建$ injector的新实例,然后 用于解析参考.

The inject function wraps a function into an injectable function. The inject() creates new instance of $injector per test, which is then used for resolving references.

因此,要回答您的问题,不,这两种方式确实没有区别,除了(我认为),使用inject函数获取依赖关系而不是通过$injector

So, to answer your question, no, there really isn't a difference in the two ways, other than (in my opinion) it's a lot easier to just use the inject function to get dependencies instead of going through the $injector

这篇关于AngularJS中的$ injector和Inject()之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆