在angular2的一个组件中添加多个templateUrl [英] add more than one templateUrl in One component in angular2
问题描述
angular2允许使用反引号(`)编写多行html代码.
但是当使用templateUrl时,我不知道如何添加多个html文件.
angular2 allows to write muliple-line html code using back-ticks(`).
but when using templateUrl, i dont know how adding more than one html file.
当我尝试这个..
@Component({
selector: 'my-app',
template: `
<h1>view1</h1>
<h1>view2</h2>
`
})
class Appcomponent{
}
那样.
@Component({
selector: 'my-app',
templateUrl: './HTML1.html','./HTML2.html'
})
class Appcomponent{
}
连同HTML1.html和HTML2.html
together with HTML1.html and HTML2.html
HTML1.html
HTML1.html
<h1>view1</h1>
HTML2.html
HTML2.html
<h1>view2</h1>
我可以在angular2中使用多个templateUrl吗? 感谢您抽出宝贵时间阅读本:)
can i using multiple templateUrl in angular2? thanks for your time to read this :)
推荐答案
您不能添加多个HTML文件.我也看不到这将实现什么目的.
You can't add more than one HTML file. I also don't see what purpose this would fulfill.
如果您打算这样做,可以使用*ngIf
或*ngSwitchCase
仅显示模板的一部分
You can use *ngIf
or *ngSwitchCase
to show only parts of the template if that is your intention
@Component({
selector: 'my-app',
template: `
<h1 *ngIf="view == 'view1'>view1</h1>
<h1 *ngIf="view == 'view2'>view2</h2>
<button (click)="view = view == 'view1' ? 'view2' : 'view1'">toggle</button>
`
})
class Appcomponent{
view = 'view2';
}
这篇关于在angular2的一个组件中添加多个templateUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!