Grunt build task to concatenate & register your AngularJS templates in the $templateCache
NOTE:
- Use
0.1.xfor Grunt0.3.x. - Use
0.2.xor0.3.xfor Grunt0.4.x.
Install this grunt plugin next to your project's grunt.js gruntfile with: npm install grunt-angular-templates
Then add this line to your project's grunt.js gruntfile:
grunt.loadNpmTasks('grunt-angular-templates');This plugin provides the grunt task ngtemplates, which will allow you to compile your HTML templates into a single JS file,
which preloads $templateCache to prevent round-trips to the server.
// grunt.js
grunt.initConfig({
ngtemplates: {
myapp: {
options: {
base: 'src/views', // $templateCache ID will be relative to this folder
prepend: '/static/assets/' // (Optional) Prepend path to $templateCache ID
},
src: [ 'src/views/**.html' ],
dest: 'dist/templates.js'
}
}
});You should name your sub-target (e.g. myapp) after the name of the module the templates will be added to.
This will generate the following at dist/templates.js:
angular.module('myapp').run(['$templateCache', function($templateCache) {
...
}]);This can either be done via HTML:
<script src="dist/templates.js"></script>or via your Gruntfile:
concat: {
myapp: {
src: [
'src/js/**/*.js', // MyApp module first
'<%= ngtemplates.myapp.dest %>' // Generated templates
],
dest: 'dist/js/app.js'
}
}- Add
prependoption for modifying final$templateCacheIDs, thanks to @mbarchein. (#16)
- BC break - Templates are added to an existing module (e.g.
myapp) rather than being their ownmyapp.templatesmodule to be manually included, thanks to @geddesign. (#10)
-
Fixes
- Escape backslashes, thanks to @dallonf. (#9)
- Remove
./bin/grunt-angular-templates. No need for it!
- Update to Grunt 0.4, thanks to @jgrund. (#5)
-
Fixes
- Convert
\\to/in template IDs (for on win32 systems) (#3)
- Convert
- Added NPM keywords
-
Fixes
-
New
- Added directions to README on how to integrate with AngularJS app
- Integrated with TravisCI
- Released to NPM
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
Copyright (c) 2013 Eric Clemmons Licensed under the MIT license.
