我如何在 Angular2 中使用 <html> 模板?

How can I use <html> templates in Angular2?

我想用 html 文件替换我的代码内模板。我该怎么做?

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

我想要这样的东西

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

我使用 Angular2 和 ES5。

可能在您的模板中您可以引用另一个 HTML 文件,例如:

<ng-include src="'templates/[new template file].html'"></ng-include>

作为 ComponentAnnotation 的一部分,有一个 属性 templateUrl,您可以将其指向 HTML 文件。

  • 模板 - 指向原始 HTML
  • templateUrl - 指向包含 HTML
  • 的 URL

这与angular 1.x指令定义对象相同

您可以找到有关组件属性的更多信息here

注意 - 文档仍在进行中

根据 Angular 官员的规定,您可以在 ComponentAnnotationViewAnnotation 中使用 TemplateUrl 导入外部 HTML 文件。此外,您可以选择在相同的 ComponentAnnotationViewAnnotation.

中使用 Template

更新:

@view 已被弃用,因此提供 html 的唯一方法是像这样使用 @component

@component({
  ..
  template: `<h1>This is inline template </h1>`
   or
  templateUrl : 'URL FOR your html page';
  ..........
})
....