html - 如何在 Angular2 的 View 中添加没有组件的 HTML 部分

标签 html angular

我有一个组件及其 HTML 文件。

组件:

hello-world.component.ts

import {Component} from 'angular2/core';
@Component({
  selector: 'hello-world',
  templateUrl: 'hello-world.html'
})
export class HelloWorld {
  yourName: string = '';
}

模板:

hello-world.html:

<label>Name:</label>

<h1>Hello {{yourName}}!</h1>

现在,我有其他没有组件的 html template.html,例如它包含,

template.html

<h1>Welcome to template file</h1>
<p> This is an example file </p>

现在,我想在 hello-world.html 中包含 template.html 文件。我该怎么做。

我曾经在 Angular1 中这样做,

<div ng-include src="'template.html'"></div>

我怎样才能在 Angular2 中做到这一点?我对 Angular2 很陌生。

我在很多地方搜索了这个问题,每个人都给出了一个有组成部分的答案。但没有得到我需要的解决方案。

最佳答案

短:还不可能开箱即用。

你可以创建一个像旧的 ng-include 一样的 Directive ,它会为你完成工作.. 该 Directive 将加载 html 文件,然后将其粘贴到 DOM

关于html - 如何在 Angular2 的 View 中添加没有组件的 HTML 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40816903/

相关文章:

angular - 如何解决 Angular 4 中的 ngx-toastr 版本问题

angular - 正确使用 Angular TypeScript 构造函数 - 无法解析所有参数

angular - 从路由器导出导航到父路径 (NG5)

css - Angular 2没有为选择箭头div生成唯一的组件标识符

angular - 是否可以从入口组件访问服务

html - Colspan 和表格布局 :fixed break my table style

javascript - 覆盖默认浏览器表单验证

javascript - 保留、隐藏和重置基于按钮的选择选项

javascript - 在新窗口中打开链接,而不是在新选项卡中

html - Firefox 中音频标签中的 MP3