[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插件的一种压缩方式适用于大部 ...
随机推荐
- JDK版本更换后编译android系统出错
一:javac: 目标发行版 1.5 与默认的源发行版 1.7 冲突 1.设置jdk环境变量 编译android源码只支持jdk 1.6,所以如果需要编译源码必须下载jdk 1.6,不能下载最新的jd ...
- 【HDOJ】1258 Sum It Up
典型的深搜,剪枝的时候需要跳过曾经搜索过的相同的数目,既满足nums[i]=nums[i-1]&&visit[i-1]==0,visit[i-1]==0可以说明该点已经测试过. #in ...
- 先贴出代码C++ 中的单例模式
先贴出代码,代码后面是讲解 自己编写的单例模式: #include <iostream> #include <stdio.h> #include <string> ...
- 常见 jar包详解
常见 jar包详解 jar包 用途 axis.jar SOAP引擎包 commons-discovery-0.2.jar 用来发现.查找和实现可插入式接口,提供一些一般类实例化.单件的生命周期 ...
- bzoj3503
显然知道第一行就可以只道整个矩阵但n<=40,搜索是不行的,我们设第一行为x1~xm可以由轻易由第一行未知数推出第n+1行,这一步我们可以压成二进制位(因为只和奇偶有关)显然n+1行必须是0,由 ...
- Linux Kernel 本地内存损坏漏洞
漏洞名称: Linux Kernel 本地内存损坏漏洞 CNNVD编号: CNNVD-201310-663 发布时间: 2013-11-05 更新时间: 2013-11-05 危害等级: 漏洞类 ...
- BZOJ1324: Exca王者之剑
1324: Exca王者之剑 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 256 Solved: 131[Submit][Status] Desc ...
- [HDU 1254] 推箱子
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 【转】ubuntu 11.10(32位系统)下编译android源码
原文网址:http://www.cnblogs.com/dwayne/archive/2011/11/16/2251734.html 本文介绍在ubuntu 11.10系统下编译android 2.3 ...
- 配置Memcache服务器并实现主从复制功能(repcached)(转)
1.repcached介绍repcached 是日本人开发的实现memcached复制功能,它是一个单 master单 slave的方案,但它的 master/slave都是可读写的,而且可以相互同步 ...