[Whole Web] [AngularJS + Grunt] Using ng-html2js to Convert Templates into JavaScript
ng-html2js takes .html templates and converts them into strings stored in AngularJS's template cache. This allows you to bundle all of your templates into a single JavaScript file for simpler deployment and faster loading.
1. Install grunt.
2. Install grunt-html2js:
npm install grunt-html2js --save-dev
3. One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-html2js');
4. Using grunt-contrib-watch to moniter all tpl.html files in pulbic folder and register the html2js:main to the watcher.
/**
* Created by Answer1215 on 3/15/2015.
*/
module.exports = function(grunt) { grunt.initConfig({
watch: {
files: ["server/**/*.js", 'public/**/*.tpl.html'],
tasks: ['browserify', 'html2js:main']
},
html2js: {
options: {
base: 'public',
module: 'app.templates', /*Create a new module called app.tempaltes*/
singleModule: true, /*For all templates just create a single module*/
useStrict: true,
htmlmin: {
collapseBooleanAttributes: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
}
},
main: {
src: ['public/**/*.tpl.html'],
dest: 'build/templates.js'
}
}
}); grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-html2js');
}
So, once your tpl files has been changed, grunt task will run and create template js file.
5. Include your template.js to the index.html:
<script src="../build/templates.js"></script>
6. Inject the app.template module:
angular.module("app", ["ui.router", 'app.templates'])
.config(function config($stateProvider) {
$stateProvider.state("answer", {
url: "",
views: {
'home@': {
templateUrl: "home/home.tpl.html"
},
'visit@': {
templateUrl: "visit/visit.tpl.html"
}
}
})
});
7. Test code:
<!-- index.html --> <!DOCTYPE html>
<html ng-app="app">
<head>
<title>Egghead.io Tutorials</title>
<link rel="shortcut icon" href="favicon.ico">
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.11/angular-ui-router.js"></script>
<script src="../build/templates.js"></script>
<script src="ap.js"></script>
</head>
<body> <section ui-view="home"></section>
<nav ui-view="visit"></nav>
</body>
</html>
<!-- home/home.tpl.html --> <h1>Hello World, Grunt-html2js!!</h1>
<!-- visit/visit.tpl.html --> <h2>Visit!</h2>
build/template.js:
angular.module('app.templates', []).run(['$templateCache', function($templateCache) {
"use strict";
$templateCache.put("home/home.tpl.html",
"<h1>Hello World, Grunt-html2js!!</h1>");
$templateCache.put("visit/visit.tpl.html",
"<h2>Visit!</h2>");
}]);
More:
https://egghead.io/lessons/angularjs-using-ng-html2js-to-convert-templates-into-javascript
http://g00glen00b.be/angular-grunt/
https://github.com/karlgoldstein/grunt-html2js
[Whole Web] [AngularJS + Grunt] Using ng-html2js to Convert Templates into JavaScript的更多相关文章
- angular 中怎么获取路径上的参数 参考:https://docs.angularjs.org/api/ng/service/$location
参考: https://docs.angularjs.org/api/ng/service/$location
- AngularJS——grunt神器的安装
前言: 刚开始学 angularJS,在慕课网上看的大漠老师的视频(http://www.imooc.com/learn/156),里面刚开始讲述了前端开发-调试-测试所使用的手段和工具,本人对前端开 ...
- AngularJs——grunt神器的使用
前面我们已经知道了如何安装grunt,本章节给各位道友介绍如何使用 grunt 的插件,grunt是重点在于如何配置使用 Gruntfile.js,官网上也有很多范例. 1,包装函数 module.e ...
- Angularjs checkbox的ng属性
angularjs 默认给 input[checkbox] 元素定制了一些属性,如: <input type="checkbox" ng-mudel="name&q ...
- 发现个很有意思的angularjs +grunt 复习项目
最近作运维工作 docker 接触到一个开源webui dockerui 原项目地址 https://github.com/crosbymichael/dockerui 用angular框架实现,项目 ...
- 【AngularJs】---"Error: [ng:areq] Argument 'fn' is not a function, got undefined"
项目中把controller.service抽取出来 一步一步没有报错 index那里加 <script src="js/controllers/XXController.js&quo ...
- [Whole Web] [AngularJS] Localize your AngularJS Application with angular-localization
It is best to start your application's localization efforts early in development, even if you only s ...
- Grunt 自动化部署之css、image、javascript、html压缩Gruntfile.js配置
grunt.initConfig方法 用于模块配置,它接受一个对象作为参数.该对象的成员与使用的同名模块一一对应. 每个目标的具体设置,需要参考该模板的文档.就cssmin来讲,minify目标的参数 ...
- Grunt 使用(二)uglify插件压缩javascript代码
本文在配置grunt基本环境的基础下,讲解如何使用grunt-contrib-uglify进行javascript压缩 本文只介绍了grunt-contrib-uglify插件的一种压缩方式适用于大部 ...
随机推荐
- NSMutableArray,NSMutableDictionary的内存管问题
今天做项目遇到一个问题,在一个类中定义了一个可变数组,使用的是copy的内存管理策略 当往数组中添加包装好的基本数据的时候,程序直接崩溃了.解决方法:把copy换成strong就不会崩溃了; 后来做了 ...
- java区分大小写,使用TAB进行缩进,public类名只能有一个,而且文件名与类名保持一致.
java的类必须大写 java区分大小写,使用TAB进行缩进,public类名只能有一个,而且文件名与类名保持一致. 在dos用上下箭头,调用已用过的命令
- javascript的族家族史
JavaScript 实现 完整的 JavaScript 实现是由以下 3 个不同部分组成的:ECMAScript.文档对象模型.浏览器对象模型.这也就是说 cocos2d-js 中 其实我们用的是 ...
- ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签
一.简介 1.在main.xml中用TabHost.TabWidget.FrameLayout标签作布局 2.在MainActivity中生成TabHost.TabSpec,调用setIndicato ...
- android viewpager change adapter ---在使用viewpager设置新的adapter的时候发现页面还是显示旧的adapter中的值
有一个需求是当用户选择navigationview中的某一项时,右边的viewpager需要动态切换不同的adapter 发现直接setAdapter没有任何反应,加载的数据还是旧的数据 折腾了半天只 ...
- 如何查看自己运行ubuntu是32位还是64位
当安装ubuntu在pc上,不推荐在32位pc安装64位操作系统,64位pc安装32位操作系统 方法/步骤 按ctrl+shift+t 快捷键,打开终端,输入sudo uname --m ,按下ent ...
- Java 异常类层次结构
在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中. 下面的图展示了Java异常类的继承关系. 图1 粉红色的是受检查的异常(checked exceptions),其必须被 ...
- BASE64编码规则及C#实现
一.编码规则 Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需要编码的数据拆分成字节数组.以3个字节为一组.按顺序排列24位数据,再把这24位数据分成4组 ...
- Prism的IEventAggregator事件聚合器, 事件订阅发布, ViewModel之间的通讯
WPF中时常会遇到ViewModel之间的通讯,ViewModel并不知道自己的View,但是一个View发生的更改需要通知另外一个View. 举一个例子,软件界面上有个人信息,打开一个界面更改用户的 ...
- [转]ASP.NET MVC 入门3、Routing
在一个route中,通过在大括号中放一个占位符来定义( { and } ).当解析URL的时候,符号"/"和"."被作为一个定义符来解析,而定义符之间的值则匹配 ...