Aaron Aaron - 4 months ago 7
Javascript Question

How can I use <html> templates in Angular2?

I want to replace my in-code template with html file. How can I do it?

Greeting.annotations = [
new angular.ComponentAnnotation({
selector: 'greeting'
}),
new angular.ViewAnnotation({
template: '<h1>Hello!</h1>'
})
];


I want something like this

template: 'path/to/my/template.html'


I use Angular2 and ES5.

Answer

As part of the ComponentAnnotation there is a property templateUrl which you can point at an HTML file.

  • template - points at raw HTML
  • templateUrl - points at a URL containing the HTML

This is the same as angular 1.x directive definition object

You can find more information about component's properties here

NOTE - docs are still a work in progress