Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally create either text or striked through text using ternary

Tags:

angularjs

html ..

<tr ng-repeat="player in players">
    <td ng-cloak>{{ player.standing ? player.name : '<strike>' + player.name + '</strike>' }}</td>
    <td ng-cloak>{{ player.associatedNumber }}</td>
    <td ng-cloak>
        <span ng-class="player.standing === true ? 'label label-success': 'label label-danger'">{{ player.standing }}</span>
    </td>
</tr>

Dataset ..

[{
    "name": "Robert C",
    "associatedNumber": 21,
    "standing": true
}, {
    "name": "Joey C",
    "associatedNumber": 55,
    "standing": true
}, {
    "name": "Bobby A",
    "associatedNumber": 15,
    "standing": true
}]

This is the first row rendered (others are similiar) ..

screenshot

Instead I want to render either the player's name as plain text or striked through if they're not standing.

like image 412
bobbyrne01 Avatar asked Dec 04 '25 11:12

bobbyrne01


2 Answers

You need to use ng-class for this. There are two ways of writing a ternary in Angular.

Prior to version 1.1.5

<td ng-cloak data-ng-class="player.standing ? 'null' : 'strikethrough'">{{ player.name }}</td>

Version 1.1.5 and later:

<td ng-cloak data-ng-class="player.standing && 'null' || 'strikethrough'">{{ player.name }}</td>

Add the CSS style for .strikethrough and everything is good to go.

.strikethrough {
  text-decoration: line-through;
}

You can see it working at this plunker: http://plnkr.co/edit/MYnXLwCC7XI1MrvcI5ti?p=preview

like image 179
buzzsaw Avatar answered Dec 07 '25 03:12

buzzsaw


For Angular 7 you can use ngClass which adds or removes css classes on HTML elements.

.html

<td [ngClass]="player.standing ? 'null' : 'strikethrough'"> {{ player.name }} </td>

.css

.strikethrough {
    text-decoration: line-through;
}

Read more on ngClass here

like image 27
Star.Kid Avatar answered Dec 07 '25 04:12

Star.Kid



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!