码迷,mamicode.com
首页 > Web开发 > 详细

[AngularJS + Webpack] Requiring Templates

时间:2015-09-08 18:35:30      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

With Angular, most of the time you‘re specifying a templateUrl for your directives and states/routes. This means you need to make sure that you‘re loading in these templates into the $templateCache for your tests and production. Oh, and don‘t forget to update all your URLs whenever you move the files around! When you add in Webpack and the html-loader, you don‘t need to do this anymore. Simply require the html file and your work is done!

 

Install:

npm install -D html-loader

 

webpack.config.js:

module.exports = {
    entry: {
        app: [‘./app/index.js‘]
    },
    output: {
        path: ‘./build‘,
        filename: ‘bundle.js‘
    },
    module: {
        loaders: [
            {test: /\.js$/, loader: ‘babel-loader‘, exclude: /node_modules/},
            {test: /\.html$/, loader: ‘html-loader‘, exclude: /node_modules/}
        ]
    }
};

 

hello.js:

export default (ngModule) => {
    ngModule.directive(‘hello‘,  () => {
        return {
            restrict: ‘E‘,
            scope: {},
            template: require(‘./hello.html‘),
            controllerAs: ‘vm‘,
            controller: function() {
                var vm = this;
                vm.greeting = "Hello";
            }
        }
    })
}

 

[AngularJS + Webpack] Requiring Templates

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/4792446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!