Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've found a possible solution where I don't need to know about the controller before bootstrapping:</p> <pre><code>// Make module Foo and store $controllerProvider in a global var controllerProvider = null; angular.module('Foo', [], function($controllerProvider) { controllerProvider = $controllerProvider; }); // Bootstrap Foo angular.bootstrap($('body'), ['Foo']); // .. time passes .. // Load javascript file with Ctrl controller angular.module('Foo').controller('Ctrl', function($scope, $rootScope) { $scope.msg = "It works! rootScope is " + $rootScope.$id + ", should be " + $('body').scope().$id; }); // Load html file with content that uses Ctrl controller $('&lt;div id="ctrl" ng-controller="Ctrl" ng-bind="msg"&gt;').appendTo('body'); // Register Ctrl controller manually // If you can reference the controller function directly, just run: // $controllerProvider.register(controllerName, controllerFunction); // Note: I haven't found a way to get $controllerProvider at this stage // so I keep a reference from when I ran my module config function registerController(moduleName, controllerName) { // Here I cannot get the controller function directly so I // need to loop through the module's _invokeQueue to get it var queue = angular.module(moduleName)._invokeQueue; for(var i=0;i&lt;queue.length;i++) { var call = queue[i]; if(call[0] == "$controllerProvider" &amp;&amp; call[1] == "register" &amp;&amp; call[2][0] == controllerName) { controllerProvider.register(controllerName, call[2][1]); } } } registerController("Foo", "Ctrl"); // compile the new element $('body').injector().invoke(function($compile, $rootScope) { $compile($('#ctrl'))($rootScope); $rootScope.$apply(); }); </code></pre> <p><a href="http://jsfiddle.net/MzseV/5/" rel="noreferrer">Fiddle</a>. Only problem is that you need to store the <code>$controllerProvider</code> and use it in a place where it really shouldn't be used (after the bootstrap). Also there doesn't seem to be an easy way to get at a function used to define a controller until it is registered, so I need to loop through the module's <code>_invokeQueue</code>, which is undocumented.</p> <p><strong>UPDATE:</strong> To register directives and services, instead of <code>$controllerProvider.register</code> simply use <code>$compileProvider.directive</code> and <code>$provide.factory</code> respectively. Again, you'll need to save references to these in your initial module config.</p> <p><strong>UDPATE 2:</strong> <a href="http://jsfiddle.net/MzseV/7/" rel="noreferrer">Here's a fiddle</a> which automatically registers all controllers/directives/services loaded without having to specify them individually.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload