Skip to content

Instantly share code, notes, and snippets.

@raphaelluchini
Created December 23, 2014 19:40
Show Gist options
  • Save raphaelluchini/53d08ed1331e47aa6a87 to your computer and use it in GitHub Desktop.
Save raphaelluchini/53d08ed1331e47aa6a87 to your computer and use it in GitHub Desktop.
[Angularjs Async] How to load controllers in templates and directives asynchronous with webpack/commonJs
function NavBar($http) {
this.date = new Date();
this.getHttp = function(){
console.log($http);
}
};
module.exports = NavBar;
.directive('navbarController', function($http){
return{
restrict:'A',
link:function(scope, element, attrs){
scope[attrs.ngControllerUrl] = null;
var load = require('bundle?lazy!./controller.js');
load(function (file) {
scope.$apply(function(){
scope[attrs.ngControllerUrl] = new file($http);
})
});
}
<div navbar-controller="app">
Date: {{app.date}}
<a href="#" ng-click="app.getHttp()">Get $http servicer</a>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment