Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

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:

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]);

});

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.

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.

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,

share|improve this question
up vote 2 down vote accepted

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.

This will be possible in the future of AngularJS as Vojta Jina explains in this Youtube video.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.