问题描述
有没有办法在angular2中动态加载模板?在angular1中,我使用ng-include在主控制器视图中加载不同的 html
模板。我知道angular2只能接受1个templateUrl并且在angular2中搜索 ng-include
并且找不到任何引用。
Is there a way to load templates dynamically in angular2? In angular1 I used ng-include to load different html
template in the main controller view. I know that angular2 can only take 1 templateUrl and been googling ng-include
in angular2 and can't find any reference.
推荐答案
为什么你需要ng-include,当你可以将html作为自定义新组件并在任何需要的时候使用它,这要归功于选择器标签。
Why do you need ng-include when you can make the html as a custom new component and use it wherever you need it, thanks to the selector tag.
例如:
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html'
})
所以在这里, my-component.component.html
文件可以包含要包含的标记或在其他地方显示。
和选择标记值< app-my-component>< / app-my-component>
可以在整个应用标记html文件中使用(你可以把它想象成你的日常html标签 - 但是在性质上高度定制并且由角度框架解释,首先是自定义标签,但是使用角度构建。)
So here, the my-component.component.html
file can have the markup you want to include or show everywhere else.and the selector tag value <app-my-component></app-my-component>
can be used throughout your application markup html files(you can think of it as your everyday html tags--but highly customized in nature and interpreted by the angular framework first aka custom tags but built using angular).
这篇关于Angular2动态模板或ng-include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!