[AngularJS + Webpack] require directives

时间:2022-04-01 02:13:10

[AngularJS + Webpack] require directives

direictives/index.js:

module.exports = function(ngModule) {
//register all the directives here
require('./hello')(ngModule);
};

directives/hello.js

module.exports = function(ngModule) {
ngModule.directive('webHello', function() {
return {
restrict: 'E',
scope: {},
templateUrl: 'directives/hello.html',
controller: function() {
var vm = this; vm.greeting = "Hello Webpack!";
},
controllerAs: 'vm'
}
})
};

directives/hello.html:

<h1>
{{vm.greeting}}
</h1>

app/index.js:

var angular = require('angular');
var ngModule = angular.module('app', []);

//require directives folder, then it will find index.js file
require('./directives')(ngModule); console.log(ngModule);

app/index.html:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Webpack + AngularJS</title>
</head>
<body ng-app="app">
<web-hello></web-hello>
</body>
<script src="../build/bundle.js"></script>
</html>