[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>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。