AngularJS中的$injector.instantiate
,$injector.get
和$injector.invoke
之间有什么区别?
12
A
回答
20
鉴于以下服务:
app.service('myService', function ($q, $http) {
return {
q: $q,
http: $http
};
});
$ injector.get(名称,[来电显示]);
返回所请求服务的实例。
$injector.get('myService');
// { q: $q, http: $http }
$ injector.invoke(FN,[自],[当地人]);
调用提供的方法并从$ injector中传递给定的参数。
$injector.invoke(function (myService, $http) {
console.log(myService); // { q: $q, http: $http };
console.log(this); // { v: 'im this!' };
console.log($http); // null
}, { v: 'im this!' }, { $http: null });
$ injector.instantiate(类型,[当地人]);
创建给定类型的新实例。接受构造函数,然后使用构造函数注释中指定的参数调用新实例。
假设以下 '类':
function Person (fName, lName, $http, $q) {
return {
first_name: fName,
last_name: lName,
http: $http,
q: $q
}
}
现在,如果我们想在我们的控制器创建新的人,我们能做到这一点是这样的:
app.controller('...', function ($injector) {
var $http = $injector.get('$http');
var $q = $injector.get('$q');
var p = new Person('kasper', 'lewau', $http, $q);
console.log(p); // { first_name: 'kasper', last_name: 'lewau', http: $http, q: $q };
});
想象Person
有大约20个左右的依赖关系,并且我们用$injector.get
方法取得了每个人。
繁琐!和 - 你需要保持参数&参数同步。 ugh。
相反,你可以这样做:
app.controller('...', function ($injector) {
var p = $injector.instantiate(Person, {
fName: 'kasper',
lName: 'lewau'
});
console.log(p); // { first_name: 'kasper', last_name: 'lewau', http: $http, q: $q };
});
而且 - 如果我们愿意,我们可以提供当地人到.instantiate
呼叫,以便覆盖哪些内部$injector.get()
通常会得到实例化时。
var p = $injector.instantiate(Person, {
fName: 'kasper',
lName: 'lewau'
}, { $http: 'Nothing!', $q: 'Nothing!' });
console.log(p); // { first_name: 'kasper', last_name: 'lewau', http: 'Nothing!', q: 'Nothing!' };
我希望解释三者之间的区别。如果您需要更多关于他们的差异的详细信息,我建议这些文章:
- http://taoofcode.net/studying-the-angular-injector/
- http://taoofcode.net/studying-the-angular-injector-annotate/
- http://taoofcode.net/studying-the-angular-injector-invoke/
- http://taoofcode.net/studying-the-angular-injector-getservice/
- http://taoofcode.net/studying-the-angular-js-injector-instantiate/
0
附加痕卡斯帕Lewau的回答,因为我无法添加评论。
$ injector.invoke(fn,[self],[locals],[serviceName]);
- 可用于任何函数:类构造函数或没有(它具有内部自动检查)
- “自” param所仅用于纯函数。
$ injector.instantiate(Type,[locals]);
- 应仅用于类构造函数,否则将构建空对象{}
相关问题
- 1. AngularJS in HEAD vs BODY
- 2. ECB vs global vs cscope .. in emacs
- 3. template.find()vs document.querySelector vs jquery vs template。$ performance in Meteor
- 4. &vs @ and = in angularJS有什么区别
- 5. AngularJS $ injector.invoke - 没有定义ParentController
- 6. Datagrid in vs 2010
- 7. concat in FSharp.Core.String vs Concat in System.String
- 8. inline asm in C++ in vs __asm
- 9. python in applescript:subprocess.call vs os.system in automator
- 10. AS3 - for(... in ...)vs for each(... in ...)
- 11. 用户vs sudo vs sudo_user in ansible playbooks
- 12. ion-list vs ion-scroll vs virtualScroll in ionic 3
- 13. “*”vs“:”in R for model
- 14. map vs hash_map in C++
- 15. http.get vs http.request in Angular
- 16. Casex vs Casez in Verilog
- 17. ArrayBuffer vs ArrayBuilder in scala
- 18. NOT EXISTS VS NOT IN
- 19. string&vs. wstring&in runtime_error
- 20. IOLoop.add_callback vs IOLoop.add_timeout in Tornado?
- 21. instance_eval vs class_eval in module
- 22. ProvisionsAllDevices vs ProvisionedDevices in embedded.mobileprovision
- 23. float vs int in cuda
- 24. model.predict_classes vs model.predict_generator in keras
- 25. Moshi vs Gson in android
- 26. display.newImage()vs display.newImageRect()in corona
- 27. formmethod.post vs formmethod.get in mvc3?
- 28. $(this)vs this in jQuery
- 29. fs.watch vs setInterval in node.js
- 30. task.continuewith vs dataflow in c#
没有$调用,获得使用$调用,initialte()和$ invoke.invoke()? –
这一刻我没有源头在我面前,但我认为是的。检查源代码的朋友。 –
这个答案应该是Angular $注入器文档的一部分 –