2016-04-21 76 views
4

有沒有什麼方法可以爲angular2中的特定組件創建多個模板或子模板?Angular2子模板?

打字稿文件 - >

@Component({ 
    selector: 'portal', 
    templateUrl: 'main.html' 
}) 

export class AppComponent{} 

main.html中 - >

<div> 
<div>bla bla bla </div> 
    -- load sub html page ?????? 
<div> 
+0

你到底想達到什麼目的? – kemsky

+0

創建一個頭組件並從AppComponent傳遞參數。 – Cihan

回答

4

我會使用的組件這一點,因爲Angular2提供了一個基於組件的方法:

<div> 
<div>bla bla bla </div> 
    <someOtherComponent></someOtherComponent> 
<div> 

以下代碼:

@Component({ 
    selector: 'someOtherComponent', 
    templateUrl: 'other.html' 
}) 
export class SomeOtherComponent{} 
+0

你是什麼意思? –

0

目前尚不支持此功能,但有計劃最終添加支持。雖然使用@View()裝飾者的原始方法已被刪除(另請參閱https://github.com/angular/angular/issues/7363)。

您可以使用ngSwitchngIf在單個模板的不同部分之間切換。

>=RC.2

template: ` 
    <div [ngSwitch]="value"> 
     <div *ngSwitchCase="phone"> 
     phone content here 
     </div> 
     <div *ngSwitchCase="tablet"> 
     table content here 
     <div *ngSwitchDefault> 
     brower content here 
     </div> 
    </div> 
` 

<=RC.1

template: ` 
    <div [ngSwitch]="value"> 
     <div *ngSwitchWhen="phone"> 
     phone content here 
     </div> 
     <div *ngSwitchWhen="tablet"> 
     table content here 
     <div *ngSwitchDefault> 
     brower content here 
     </div> 
    </div> 
`