9

I am applying a number filter to the result of a function:

<tr class="text-center">
    <th class="text-center">{{monthCategoryTotalPredict = getCategoryMonthTotal(costDirection, month, category, "predict")  | currency:currencySymbol}}</th>
    <th class="text-center">{{monthCategoryTotalActual  = getCategoryMonthTotal(costDirection, month, category, "actual")   | currency:currencySymbol}}</th>
    <th class="text-center">{{calculateSurplus(monthCategoryTotalPredict, monthCategoryTotalActual)  | currency:currencySymbol}}</th>
</tr>

Negative numbers are represented like this by default: (£230.00).

My aim is to make them also change colour to red. How can I do this in Angular JS? Can this be part of the filter? Can I override the filter to modify it's default behaviour slightly without a complete rewrite?

Thanks in advance!

1
  • Use ng-class. You can set the class dynamically based on an expression, like monthCategoryTotalPredict >= 0. Commented Dec 30, 2013 at 17:27

5 Answers 5

12

In order to change the color of text in HTML, you'll need to modify it's container element. Since the filter doesn't know about the element, you can either inject one (bad idea), or use a directive instead of a filter.

Putting a function in your code is actually a bad way to handle things. It may have to fire multiple times, and will certainly screw up any kind of sorting you attempt.

<th class="text-center" ng-class="{ red: calculateSurplus(monthCategoryTotalPredict, monthCategoryTotalActual) < 0 }">{{calculateSurplus(monthCategoryTotalPredict, monthCategoryTotalActual)  | currency:currencySymbol}}</th>

I would honestly perform these calculations ahead of time, so that it looks like this.

<th class="text-center" ng-class="{ red: surplus < 0 }">{{surplus | currency:currencySymbol}}</th>
Sign up to request clarification or add additional context in comments.

Comments

5

You can use ng-class to define conditional classes. You can create a css class which presents numbers as red and use it in ng-class attribute.

for example;

<td ng-class="{'className': variable < 0}">{{variable}}</td>

detailed documentation : http://docs.angularjs.org/api/ng.directive:ngClass

Comments

4

You can also use ng-style and add a function to your controller.

for example:

$scope.negativeValue=function(myValue){
  var num = parseInt(myValue);
  
  if(num < 0){
    var css = { 'color':'red' };
    return css;
  }
}
<td ng-style="negativeValue(myScopeValue)">{{ myScopeValue | currency }}</td>

1 Comment

thanks, I used this but with ng-class instead of ng-style
2

Try

 <th class="text-center" ng-class="{myRedClass: monthCategoryTotalPredict  <0}" >
      {{monthCategoryTotalPredict = ......}}</th>

Then add a css class rule to change color

Comments

0

That's how it did it in my app.

All of the solutions above would recalculate the value, which could be inefficient, if it is resulted by a function.

angular.module("myApp", [])
  .controller("myCtrl", myCtrl)
  .directive("colorUp", colorUp)

function myCtrl($scope) {
  $scope.num1 = 1
  $scope.num2 = -1
  $scope.num3 = 0
}

function colorUp() {
	return {
		restrict: "A",
		link: linkFunc
	}

	function linkFunc (scope, element, attributes) {
		//adding an event handler to see elements' value changes
		element.on("DOMSubtreeModified", onValChange)

		function onValChange () {
			var eleVal = deComma(element.text())
			var color  = (eleVal > 0) ? "green": (eleVal < 0) ? "red": ""
			element.attr("class", "ng-binding " + color)
		}
		
		function deComma(text) { 
		  return (text ? (text+"").replace(/,/g, "") : "")
		}
	}
}
<!DOCTYPE html>
<html>

  <head>
    <script src="https://code.angularjs.org/1.4.8/angular.js"></script>
    <script src="script.js"></script>
    <style>
    .red {color: red;}
    .green {color: green;}
     input { width : 50px;}
    </style>
  </head>

  <body ng-app="myApp" ng-controller="myCtrl">
    <h3>Reusable Custom directive for coloring nums.</h3>
    <input type="number" ng-model="num1" placeholder="Enter first number here!!">
    <label color-up>{{num1 | number:2}}</label>
    <br>
    <input type="number" ng-model="num2" placeholder="Enter second number here!!">
    <label color-up>{{num2 | number:2}}</label>
    <br>
    <input type="number" ng-model="num3" placeholder="Enter second number here!!">
    <label color-up>{{num3 | number:2}}</label>
  </body>

</html>

here is a plunker link:- http://plnkr.co/edit/X42mE9LpagGRrasOckqQ?p=preview

Hope it helps!!

Comments

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.