Created
December 23, 2014 19:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function NavBar($http) { | |
this.date = new Date(); | |
this.getHttp = function(){ | |
console.log($http); | |
} | |
}; | |
module.exports = NavBar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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); | |
}) | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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