0

Based on the result the number itself should have a specific color. When the number is negative, the boolean should give them a classname negative so the number will be red else classname positive which is a green color.

.negative {
  color: red;
}

.positive {
  color: green;
}
<tr>
  <td>Totale Inkomsten</td>
  <td ng-model="totalcost">{{ totalCost() }}</td> 
  <td>Totale Uitgaven</td>
  <td ng-model="totalexpense">{{ totalExpense() }}</td>
</tr>

<h2 ng-class="totalcost < totalexpense? 'negative': 'positve'">{{ totalCost() - totalExpense() }} </h2>
tanmay
  • 7,761
  • 2
  • 19
  • 38

1 Answers1

1

With Angular 2, the way of using ngClass has changed. Now you should use it as a property binding like :

<h2 [ngClass]="{'negative': totalcost < totalexpense, 'positve' : totalcost > totalexpense">{{ totalCost() - totalExpense() }} </h2>

If you have other things in place, this should give you the desired result.

Saurabh Tiwari
  • 4,632
  • 9
  • 42
  • 82
  • I assume the binding does not give an error, but it would not assign the className to it... –  May 05 '17 at 06:41
  • It should assign the class as long as the class is properly defined and the corresponding css file is loaded. Try checking if your class is applied or not by inspecting the element. May be its getting overridden somewhere. – Saurabh Tiwari May 05 '17 at 06:43
  • 1029.05 –  May 05 '17 at 06:44
  • This should not be the case normally. Are you having any console errors. ? – Saurabh Tiwari May 05 '17 at 06:46
  • console is clean as hell :P –  May 05 '17 at 06:47
  • This `

    1029.05

    ` is fine. And this also means that your class object got applied. I just realized you had a different spelling of `positive` in html and css. Can that be the case. Check with a negative value once
    – Saurabh Tiwari May 05 '17 at 06:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143462/discussion-between-fronteerder-and-saurabh-tiwari). –  May 05 '17 at 06:54