2017-06-15 7 views
1

私は疑問があり、解決方法はわかりません。サーバーからデータを取り出してテーブルに表示します。これらのフィールドの1つは、 'OK'、 'ERROR'または 'CANCEL'のような値を持つ文字列です。値に応じていくつかのブートストラップクラスを割り当てることは可能ですか?たとえば、「OK」の場合はbg-succes、「CANCEL」の場合はbg-dangerです。角度2 - 文字列の値に依存するブートストラップクラスを設定する

例:ところで

<table class="table table-hover"> 
    <thead class="thead-inverse"> 
     <tr> 
      <th class="text-center"><strong>Date 1/ Date 2</strong></th> 
      <th class="text-center"><strong>Status</strong></th> 
      <th class="text-center"><strong>Date 3</strong></th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr *ngFor="let item of items"> 
      <td class="text-center"> 
       <tr class="text-center"> {{item.Date1}} </tr> 
       <tr class="text-center"> {{item.Date2}} </tr> 
      </td> 
      <td class="text-center"> 
       <tr>Status</tr> 
       <tr class="bg-success"> {{item.Status}}</tr>//Want to assign here the class     
      </td> 
      <td class="text-center"> {{item.Date3}} </td>    
     </tr> 
    </tbody> 
</table> 

ありがとう!

答えて

2

ngClassディレクティブを使用しても同じ結果が得られます。

<tr [ngClass]="getStatusClass(item.Status)"> {{item.Status}}</tr> 

コード

getStatusClass(status: string){ 
    let statuses = {"OK": "bg-success", "ERROR": "bg-danger", "CANCEL": "bg-warning"} 
    return statuses[status] || 'bg-default'; 
} 
+1

ように注意してくださいしかし、角度は大文字と小文字が区別されます。あなたのキーはOK、ERROR、CANCEL、ok、error、cancelではありません。 – trichetriche

+0

それは動作します!しかし、配列の最初の要素だけです。/ – Aw3same

+1

@trichetriche角についてではなく、ここでは辞書キーで大文字と小文字が区別されます:pリストを更新しましたが、頭がおかしくなりました.. –

関連する問題