2017-08-11 11 views
0

私は配列があります。<thead/>で反復処理中にテンプレートを選択する方法は?

let headers = [ 
    { title: 'First Name', style: 'bold' }, 
    { title: 'Last Name', style: 'bold' },    
    { title: 'Book', style: 'bold', titleSecond: 'All/Remainder', style2:'not bold' }, 
    { title: 'City', style: 'not bold', titleSecond: 'Street', style2: 'not bold' } 
    ]; 

を、私は、単純なHTMLテーブルを反復処理しています:

<table> 
    <thead> 
     <tr> 
      <th *ngFor="let header headers"> 
       {{ header.title }} 
      </th> 
     </tr> 
    </thead> 
</table> 

は次のようになりますテーブルヘッダを作成することが可能です:

enter image description here

テーブルヘッダーのテンプレートをいくつか作成できますか?このheadersオブジェクトを変更したいので、オブジェクトを変更することができます。

+0

問題は何ですか?なぜあなたの4番目のオブジェクトは重複したプロパティ 'style'を持っていますか? – yurzui

+0

@yurzuiおっと、申し訳ありませんが、私はオブジェクトを編集しました。 – StepUp

答えて

1

あなたは条件付きで再利用可能なコンポーネントの中で、この特定のロジックを作りたい場合は、子コンポーネントを作成する必要が* ngIf

<th *ngFor="let header headers"> 
<span *ngIf="header.style==='bold'"> <b>{{ header.title }}<b></span> 
<span *ngIf="header.style!='bold'"> {{ header.title }} 

<br> {{header.titleSecond}} 
</th> 

を使用して、大胆に変更することができます子コンポーネントにヘッダーを渡します。子コンポーネントからは

<th *ngFor="let header headers"> 
    <your-childcomponent [header]="header"></your-childcomponent> 
    </th> 

子コンポーネント

 import { Component, Input, } from '@angular/core'; 
@Component({ 

    selector: 'your-childcomponent', 
    templateUrl: "YourChildComponent.html" 
}) 
     export class YourChildComponent { 
      @Input() header:any; 

     } 

@Input で値を取得しますYourChildComponent.html

<span *ngIf="header.style==='bold'"> <b>{{ header.title }}<b></span> 
    <span *ngIf="header.style!='bold'"> {{ header.title }}</span> 
<br> {{header.titleSecond}} 

私はあなたが期待しているこのことを願っています

+0

シンタックスを更新する '

+0

@Yatinpatelありがとう。私の間違い – JEMI

+0

いつでも、歓迎します:) –

関連する問題