2016-11-30 81 views
2

angular2允许使用back-ticks(`)编写多行html代码。
但使用templateUrl时,我不知道如何添加多个HTML文件。在angular2中的一个组件中添加多个templateUrl

当我尝试这个..

@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

一起

HTML1.html

<h1>view1</h1> 

HTML2.html

<h1>view2</h1> 

我可以使用多个templateUrl在angular2? 感谢您的时间阅读:)

+0

这是客观的?为什么你需要2个html文件? – Picci

+0

我试图为编辑源代码方便地分长的HTML文件..但意识到它是没用的.. thx :) – highalps

+1

你可能想考虑是否需要划分长的HTML只是一个迹象表明需要重构代码并创建更小的组件 – Picci

回答

0

您不能添加多个HTML文件。我也看不到这会达到什么目的。

您可以使用*ngIf*ngSwitchCase显示模板的唯一部分,如果这是你的意图

@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'; 
} 
+0

谢谢!我意识到这是不必要的工作 – highalps