Angular Template loader for webpack
Puts HTML partials in the Angular's $templateCache so directives can use templates without initial downloading.
Webpack and loaders
Webpack is webpack and it's module bundler. Loaders wrap content in the javascript code that executes in the browser.
Install
npm install ng-cache-loader
Usage
You can require html partials via ng-cache-loader
:
require('ng-cache!./demo/template/myPartial.html');
Partial will be available as ng-include="'myPartial.html'"
or templateUrl: 'myPartial.html'
.
Named templates
You can wrap template in the script
tag:
<script type ="text/ng-template" id="myFirstTemplate">
</script>
You can have multiple templates in one file:
<script type ="text/ng-template" id="myFirstTemplate">
</script>
<script type ="text/ng-template" id="mySecondTemplate">
</script>
You can mix named templates and simple markup:
<script type ="text/ng-template" id="myFirstTemplate">
</script>
<div>...</div>
<script type ="text/ng-template" id="mySecondTemplate">
</script>
Prefix
Prefix adds path left of template name:
require('ng-cache?prefix=public/templates!./path/to/myPartial.html')
Prefix can mask the real directory with the explicit value
or can retrieve the real directory (use [dir]
):
require('ng-cache?prefix=public/[dir]/templates!./path/to/myPartial.html')
Prefix can strip the real directory (use //
):
require('ng-cache?prefix=public//[dir]/templates!./far/far/away/path/to/myPartial.html')
webpack config
Match .html
extension with loader:
module: {
loaders: [
{
test: /.html$/,
loader: "ng-cache?prefix=[dir]/[dir]"
}
]
},
License
MIT (http://www.opensource.org/licenses/mit-license.php)