简体   繁体   English

根据条件改变角度 6 行颜色

[英]Changing angular 6 Row Colour according to condition

I am currently working with angular 6 and I have to make lines of a table change according to the content of the row.我目前正在使用 angular 6,我必须根据行的内容更改表格的行。

So i am using conditional statement with ngif .所以我在ngif使用条件语句。 I am wondering if there is a way to do that without making 2 Ngif statements?我想知道是否有办法在不做 2 个 Ngif 语句的情况下做到这一点?

You don't need angular or javascript - just CSS - the following uses the :nth-of-type(even) selector.您不需要 angular 或 javascript - 只需 CSS - 以下使用:nth-of-type(even)选择器。

Only the tr's that satisfy your ngIf condition will show and this stylning will be applied to every second row irrespective of the number of rows that don't show.只有满足 ngIf 条件的 tr 才会显示,并且无论未显示的行数如何,此样式都会应用于每隔一行。

 table { display: block; width: 100%; } tr:nth-of-type(even) { background: yellow; color: blue; }
 <table> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> <tr> <td>David</td> <td>Griffith</td> <td>50</td> </tr> </table>

I found out how to do it with ngStyle:我发现了如何用 ngStyle 做到这一点:

[ngStyle]
  <li  [ngStyle]= "{ 'backgroundColor' : (film.duree=='2 heures') ? 'blue' : 'red'  }">  {{ film.nom}}-   {{ film.duree}}-  {{ film.categorie}}</li>

除了直接标记样式之外,如果提供谓词,您可以使用 Angular 属性绑定轻松切换包含样式信息的类:

<li [class.my-class]="condition">data</li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM