问题描述
我有一个关于延迟加载的服务和工厂的问题。我已经一个多星期,现在研究这个并没有找到一个满意的答案。我发现:Lazy与RequireJS装载AngularJS模块这似乎接近,但它不是我要找的,我不希望延迟加载的模块我
I have a question about lazy loading services and factories. I've been researching this for over a week now and haven't found a satisfactory answer. I have found: Lazy loading AngularJS modules with RequireJS which seems to come close but it's not what I'm looking for, I do not want to lazy-load my modules.
我不知道如何来搭起使用AngularJS和RequireJS澄清我的需要plunker;所以我会尝试并解释清楚我的需要尽可能:
I do not know how to rig up a plunker using AngularJS and RequireJS to clarify my needs; so I will try and explain my needs as clearly as possible:
在每个时间我AngularJS的控制器,服务和工厂的这点看起来是这样的:
At this point in time each of my AngularJS controllers, services and factories looks like this:
var dependencies = ['something', 'something/else'];
define(dependencies, function() {
var controller = function(something, else) {};
angular.module('app').controller('myController', ['something', 'else', controller]);
});
这工作。我使用RequireJS载入我的依赖关系,并一切正常。是什么在困扰我的是要申报的依赖和RequireJS内换我AngularJS code定义呼叫的需要,我想AngularJS到(此处原谅我)做正确的事。
This works. I load my dependencies using RequireJS and everything is alright. What is bothering me is the need to declare my dependencies and wrap my AngularJS code within a RequireJS define call, I would like AngularJS to (excuse me here) do the right thing.
在理论上,当且仅当所有这一切都是承诺的参数都解决了执行我的控制器的构造函数。我想创建一个返回服务承诺服务解析的东西
和别的
,这样我可以跳过在RequireJS内依赖定义
调用和处理我的服务解析器的里面,甚至我的DI容器内更好。
In theory the constructor of my controller is executed if and only if all the parameters which are promises are resolved. I would like to create a service resolver which returns a service promise for something
and something else
so that I can skip the dependencies within the RequireJS define
call and handle that inside of my service resolver or even better inside of my DI container.
我的问题是,没有任何人有这种模式的解决方案?有谁知道的一种方式来改变服务和工厂的方式被加载到的承诺?或者我应该只是实现它我一直在做的方式,等待AngularJS 2.0和孩子喷油嘴?
My question is this, does anyone have a solution for this pattern? Does anyone know of a way to change the way services and factories are loaded to promises? Or should I just implement it the way I've been doing and wait for AngularJS 2.0 and the child injectors?
我希望这个问题是有道理的。感谢您的时间和帮助,
I hope this question makes sense. Thank you for your time and help,
推荐答案
目前,它是不可能改变的实施。 AngularJS没有一套方法来改变的ServiceProvider定制返回promisses,而不是具体的对象。
At the moment it is not possible to change the implementation. AngularJS does not have a set way to change the ServiceProvider to return custom promisses instead of concrete objects.
这将在AngularJS为中的。
This will be possible in the future of AngularJS as Vojta Jina explains in this Youtube video.
这篇关于使用RequireJS懒加载AngularJS DI组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!