Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apply conditional style in angular 2

I am trying to apply conditional styles for example, there is obsolete property in user, if a user's obsolete is true, I would like to add <del> like below. I searched internet, but I don't know well what is the best way. I know that *ngIf, so I could use it, but I think there is more smart way. If someone used to this, could you please let me know?

<tr *ngFor="let user of pagedItems">
           <td align="left">{{user.name}}</td>
           <td align="left">{{user.age}}</td>
</tr>


<tr *ngFor="let user of pagedItems">
           <td align="left"><del>{{user.name}}</del></td>
           <td align="left"><del>{{user.age}}</del></td>
</tr>
like image 767
Anna Lee Avatar asked Dec 20 '25 15:12

Anna Lee


1 Answers

You could do the following:

<tr *ngFor="let user of pagedItems">
    <td align="left">
        <span [class.obselete]="user.obsolete">{{user.name}}</span>
    </td>
    <td align="left">
        <span [class.obselete]="user.obsolete">{{user.age}}</span> 
    </td>
</tr>

This would apply the css class .obselete to the text if your user.obsolete = true.

CSS for line-through:

    .obselete { 
        text-decoration: line-through; 
    }
like image 177
Peza Avatar answered Dec 23 '25 13:12

Peza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!