Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am trying to integrate angularjs app with requirejs. I want to preload templates and controllers on-demand. In my example:

http://plnkr.co/edit/vIps7t92OFzA5RXoTjvI?p=preview

I init controller SocialController inside the app.js and I need to load dynamically StreamController inside the SocialController. Unfortunately I am getting an exception, see browser console.

Argument 'StreamController' is not a function, got undefined

If I remove from SocialController

angular.module('sampleTest').controller('StreamController', StreamController);

and add it to app.js, it works but in this case requirejs will preload it right at the beginning and not when I need, inside the SocialController on demand.

share|improve this question
    
Lazy loading Angular code is tricky (it shouldn't be IMO). Take a look at my take on this angular-require-lazy - there are also others around. – Nikos Paraskevopoulos Sep 24 '15 at 8:01
up vote 0 down vote accepted

Here is an answer. Looks like angular does not allow instantiating any new services or controllers later on, after the app startup. So the code below I had to add to app.js files at the bottom, after all the controller and services are init. This is a hack but fixes the problem for lazy loading with requirejs.

See my link to plunkr with the fix. Now there are no exception and StreamController is lazy loaded and init after the startup.

sampleApp.config(
            function (
                $controllerProvider,
                $compileProvider,
                $filterProvider,
                $provide
            ) {
                sampleApp.controller = $controllerProvider.register;
                sampleApp.directive = $compileProvider.directive;
                sampleApp.filter = $filterProvider.register;
                sampleApp.factory = $provide.factory;
                sampleApp.service = $provide.service;
            }
        );
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.