2

I need to define different style by the value of a champ in ng-repeat, i try this :

<thead>
    <tr><th>#</th><th>Nom</th><th>Ville</th><th style="width: 50%;">Lignée</th></tr>
</thead>
<tbody>
    <tr ng-repeat="alternate1 in alternateViewText1.features | orderBy:'fiche.id*1'">
        <td>{{alternate1.fiche.id}}</td>
        <td ng-if="alternate1.properties.statut_activite == 'yes'" style="color: green;">{{alternate1.properties.nom}}</td>
        <td ng-if="alternate1.properties.statut_activite == 'no'" style="color: red;">{{alternate1.properties.nom}}</td>
        <td>{{alternate1.properties.ville}}</td>
        <td>{{alternate1.properties.lignee}}</td>
    </tr>
</tbody>

it's not working (nothing show)

how can i define automatically a style with a particular value ?

2
  • Please provide fiddle Commented Jun 5, 2014 at 8:57
  • jsfiddle.net/bcKg6/3 Commented Jun 5, 2014 at 9:02

2 Answers 2

3

You can create a class for particular style and use ng-class like this :

JS FIDDLE

css:

.green
 {
   color:green;
 }

.red
 {
   color:red;
 }

html:

<td data-ng-class="{'green' : alternate1.properties.statut_activite == 'actif', 'red': alternate1.properties.statut_activite == 'inactif'}">{{alternate1.properties.nom}}</td>
Sign up to request clarification or add additional context in comments.

7 Comments

Why data-ng-class instead of ng-class ?
That is optional, both are same , in order to make the template valid HTML, we can use data-* attributes.
Why not combine into one td? The expression ca be something like: ng-class="{'green' : alternate1.properties.statut_activite == 'yes', 'red' : alternate1.properties.statut_activite == 'no' }".
This solution will output the name two times. To display it only once: ng-class={ 'green': ...statut_activite == 'yes', 'red': ...statut_activite == 'no' }.
ah, thats rit!, I didn't notice both are same values.
|
2

You use the wrong value in your test

jsFiddle

Replace yes by actif and no by inactif

Example :

<td ng-if="alternate1.properties.statut_activite == 'actif'" style="color: green;">{{alternate1.properties.nom}}</td>
<td ng-if="alternate1.properties.statut_activite == 'inactif'" style="color: red;">{{alternate1.properties.nom}}</td>

PS:

The answer of ssilas777 is the best way to execute this switch of css class

2 Comments

Good that you found out 'actif' and 'inactif' should be used instead but I strongly believe ng-class should be the better option for this.
Yes i know and i say this in the comment of your answere ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.