有没有办法在 angular2 中动态加载模板?在 angular1 中,我使用 ng-include 在主控制器视图中加载不同的
html
模板。我知道 angular2 只能采用 1 个 templateUrl 并在 angular2 中谷歌搜索ng-include
并且找不到任何参考。
既然可以将 html 作为自定义的新组件并在任何需要的地方使用它,为什么还需要 ng-include,这要归功于选择器标签。
例如:
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html'
})
所以在这里,
my-component.component.html
文件可以包含您想要包含或显示在其他地方的标记。
并且选择器标签值 <app-my-component></app-my-component>
可以在整个应用程序标记 html 文件中使用(您可以将其视为日常 html 标签——但本质上是高度自定义的,首先由角度框架解释,也就是自定义标签,但使用角度构建) .
不确定动态模板,但你可以插入一个动态组件(当然会有不同的模板)。
一个简单的示例,但已弃用:Angular2:在 DOM 中插入一个动态组件作为容器的子项
最新的示例,但可以在此处找到更复杂的示例:How can I use/create dynamic template to compile dynamic Component with Angular 2.0?