I have a web page with a module defined (myModule) where I\'m boostraping angularjs using
angular.bootstrap(element,[myModule.name]);
After
This Fiddle show-cases how to dynamically load/register and use:
$controllerProvider
)$compileProvider
)$templateCache
)// Initialize app.lazyController and app.lazyDirective.
// We will later use them to create controller and directives dynamically.
var app = angular.module('app', []);
app.config(function($controllerProvider, $compileProvider) {
// see page 12 of:
// http://www.slideshare.net/nirkaufman/angularjs-lazy-loading-techniques
app.lazyController = $controllerProvider.register;
// see: http://jsfiddle.net/8Bf8m/33/
app.lazyDirective = $compileProvider.directive;
});
// set of partials
var partials = [];
// store scope & templateCache, so we can dynamically insert partials
var scope, templateCache;
// define main controller
function MainCtrl($scope, $templateCache) {
$scope.partials = partials;
scope = $scope;
templateCache = $templateCache;
}
var maxPartials = 3;
var i = 0;
var timer = setInterval(function() {
var i = partials.length;
app.lazyDirective('clickMe', function () { return {
link : function (scope, element) {
element.bind('click', function () {
alert('Clicked: ' + element.text());
});
},
};});
// define controller
var ctrlName = 'partial' + i + 'Ctrl';
app.lazyController(ctrlName, function($scope) {
$scope.text = 'hi ' + i;
});
// add partial template that I have available in string form
var newPartial = {
name: 'template' + i,
content: '<div ng-controller="' + ctrlName + '" class="a' + i + '">' +
'<input type="text" ng-model="text"></input>'+
'{{text}} <br/>' +
'<button click-me="">Click Me!</button>' +
'</div> <br/> <br/>'
};
partials[i] = newPartial;
// add template and notify angular of the content change
templateCache.put(partials[i].name, partials[i].content);
scope.$apply();
// stop timer
if (partials.length >= maxPartials) clearInterval(timer);
}, 1000);
The thing with registering lazy controller or directives is that you have to get hold of $controllerProvider and $compileProvider respectively.
It can be done only during configuration phase so you have to keep the references until you load the controllers/directives.
Lately I was working on lazy loading of the controller, today I've added support for directives, check out my code here:
https://github.com/matys84pl/angularjs-requirejs-lazy-controllers/
particularly this module lazy-directives.js
NOTE: I use RequireJS in my project, however applying my solution to yepnope should be quite easy.