|
| 1 | +# Angular Template loader for webpack |
| 2 | + |
| 3 | +Puts HTML partials in the Angular's $templateCache so directives can use templates without initial downloading. |
| 4 | + |
| 5 | +## Webpack and loaders |
| 6 | + |
| 7 | +Webpack is the [webpack](http://webpack.github.io/) and it's module bundler. [Loaders](http://webpack.github.io/docs/using-loaders.html) wrap content in the javascript code that executes in the browser. |
| 8 | + |
| 9 | +# Install |
| 10 | + |
| 11 | +`npm install ng-cache-loader` |
| 12 | + |
| 13 | +# Usage |
| 14 | + |
| 15 | +You can require html partials via `ng-cache-loader`: |
| 16 | + |
| 17 | +``` javascript |
| 18 | +require('ng-cache!./demo/template/myPartial.html'); |
| 19 | +``` |
| 20 | + |
| 21 | +Partial will be available as `ng-include="'myPartial.html'"` |
| 22 | +or `templateUrl: 'myPartial.html'`. |
| 23 | + |
| 24 | +## Named templates |
| 25 | + |
| 26 | +You can wrap template in the `script` tag: |
| 27 | + |
| 28 | +``` html |
| 29 | +<!-- ./demo/template/myPartial.html --> |
| 30 | + |
| 31 | +<script type ="text/ng-template" id="myFirstTemplate"> |
| 32 | + <!-- then use ng-include="'myFirstTemplate'" --> |
| 33 | +</script> |
| 34 | +``` |
| 35 | + |
| 36 | +You can have multiple templates in one file: |
| 37 | + |
| 38 | +``` html |
| 39 | +<!-- ./demo/template/myPartial.html --> |
| 40 | + |
| 41 | +<script type ="text/ng-template" id="myFirstTemplate"> |
| 42 | + <!-- then use ng-include="'myFirstTemplate'" --> |
| 43 | +</script> |
| 44 | + |
| 45 | +<script type ="text/ng-template" id="mySecondTemplate"> |
| 46 | + <!-- then use ng-include="'mySecondTemplate'" --> |
| 47 | +</script> |
| 48 | +``` |
| 49 | + |
| 50 | +You can mix named templates and simple markup: |
| 51 | + |
| 52 | +``` html |
| 53 | +<!-- ./demo/template/myPartial.html --> |
| 54 | + |
| 55 | +<script type ="text/ng-template" id="myFirstTemplate"> |
| 56 | + <!-- then use ng-include="'myFirstTemplate'" --> |
| 57 | +</script> |
| 58 | + |
| 59 | +<!-- markup outside script tags available as ng-include="'myPartial.html'" --> |
| 60 | +<div>...</div> |
| 61 | + |
| 62 | +<script type ="text/ng-template" id="mySecondTemplate"> |
| 63 | + <!-- then use ng-include="'mySecondTemplate'" --> |
| 64 | +</script> |
| 65 | +``` |
| 66 | + |
| 67 | +## Prefix |
| 68 | + |
| 69 | +Prefix adds path left of template name: |
| 70 | + |
| 71 | +``` javascript |
| 72 | +require('ng-cache?prefix=public/templates!./path/to/myPartial.html') |
| 73 | +// => ng-include="'public/templates/myPartial.html'" |
| 74 | +``` |
| 75 | + |
| 76 | +Prefix can mask the real directory with the explicit value |
| 77 | +or retrieve the real directory name (use `[dir]`): |
| 78 | + |
| 79 | +``` javascript |
| 80 | +require('ng-cache?prefix=public/[dir]/templates!./path/to/myPartial.html') |
| 81 | +// => ng-include="'public/path/templates/myPartial.html'" |
| 82 | +``` |
| 83 | + |
| 84 | +Prefix can strip the real directory name (use `//`): |
| 85 | + |
| 86 | +``` javascript |
| 87 | +require('ng-cache?prefix=public/[dir]//[dir]/templates!./far/far/away/path/to/myPartial.html') |
| 88 | +// => ng-include="'public/far/path/templates/myPartial.html'" |
| 89 | +``` |
| 90 | + |
| 91 | +## webpack config |
| 92 | + |
| 93 | +Match `.html` extension with loader: |
| 94 | + |
| 95 | +``` javascript |
| 96 | +module: { |
| 97 | + loaders: [ |
| 98 | + { |
| 99 | + test: /.html$/, |
| 100 | + loader: "ng-cache?prefix=[dir]/[dir]" |
| 101 | + } |
| 102 | + ] |
| 103 | +}, |
| 104 | +``` |
| 105 | + |
| 106 | +# License |
| 107 | + |
| 108 | +MIT (http://www.opensource.org/licenses/mit-license.php) |
0 commit comments