简体   繁体   English

我该怎么用 <html> Angular2中的模板?

[英]How can I use <html> templates in Angular2?

I want to replace my in-code template with html file. 我想用html文件替换代码模板。 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. 我使用Angular2和ES5。

As part of the ComponentAnnotation there is a property templateUrl which you can point at an HTML file. 作为ComponentAnnotation一部分,有一个属性templateUrl ,您可以指向HTML文件。

  • template - points at raw HTML 模板-指向原始HTML
  • templateUrl - points at a URL containing the HTML templateUrl-指向包含HTML的URL

This is the same as angular 1.x directive definition object 这与angular 1.x指令定义对象相同

You can find more information about component's properties here 您可以在此处找到有关组件属性的更多信息。

NOTE - docs are still a work in progress 注意 - 文档仍在开发中

As provided by Angular officials you can import external HTML file by using TemplateUrl in ComponentAnnotation as well as in ViewAnnotation too. 正如Angular官员提供的那样,您可以通过在ComponentAnnotation以及ViewAnnotation使用TemplateUrl导入外部HTML文件。 moreover you have option to use Template in the same ComponentAnnotation and ViewAnnotation . 此外,您可以选择在相同的ComponentAnnotationViewAnnotation使用Template

update: 更新:

@view has been deprecated so only way to provide html is using @component like this @view已被弃用,因此提供html的唯一方法是使用@component这样的

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM