Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to $templateCache in Angular2

Tags:

angular

When we think of using template in Angular2 or Angular1.X, we know below is one of the basic way of writing:

template: './template-ninja.html'

and with Angular1.X, we can previously cache all of templates with $templateCache.put() as follows:

var myApp = angular.module('Ninja', []);
myApp.run(function($templateCache) {
  $templateCache.put('templateNinja.html', 'This is the content of the template-ninja');
});

which will lessen the number of http requests. I'd like to know how can I implement the same with Angular2. Could anyone help? Thanks.

like image 672
chitakuma Avatar asked Dec 22 '15 15:12

chitakuma


3 Answers

gulp-inline-ng2-template

Currently the best solution is gulp-inline-ng2-template.

This takes a component with a templateUrl attribute, plus an html file, and turns it into a component with an inlined template.

https://github.com/ludohenin/gulp-inline-ng2-template

like image 65
superluminary Avatar answered Nov 07 '22 21:11

superluminary


I don't know of a library or tool that will cache templates for Angular 2 yet, but so far, I have been happy to just use inline templates instead of external files. Using the es6 backtick character for a template, it allows you to write multiline HTML in the JavaScript and keeps everything in one file.

https://github.com/angular-in-action/chapter2/blob/master/client/components/dashboard.ts#L12

like image 2
Jeremy Wilken Avatar answered Nov 07 '22 21:11

Jeremy Wilken


gulp-ng2-template-wrap

Hi,

Hope this will be helpful to you: I just wrote a small module that allows you to bundle all of your html files into single ES6 module.

https://github.com/evgenys91/gulp-ng2-template-wrap

like image 2
Evgeny Sorokin Avatar answered Nov 07 '22 21:11

Evgeny Sorokin