1

I have the following input.

HTML is as follows.

 <input type="number" ng-class="{negative: amount < 0}" ng-model="amount"/>

CSS is as follows

 .negative {
         color: red;
 }

If the amount is positive, it will display no css, if the amount is negative, it will use .negative as its css and display the font in red color.

However i want the positive class to show up when the amount is positive in number.

Can someone let me know how to satisfy both the positive and negative values.

Here is the css for positive.

.positive {
         color: blue;
}
1

4 Answers 4

3

You can do it without a function like so

<input type="number" ng-class="{positive: amount >= 0, negative: amount < 0}" ng-model="amount"/>    

a codepen showing

Sign up to request clarification or add additional context in comments.

Comments

1

Alternatively, you could have a complex ng-class.

<input type="number" class="positive" ng-class="{'negative': amount < 0, 'positive': amount > 0, 'zero': amount == 0}" ng-model="amount"/>

Comments

1

I believe you wanted to add class on field based on value entered, You could use ng-class with function which will decide which class will get applied.

<input type="number" ng-class="decideClass(amount)" ng-model="amount"/>

$scope.decideClass = function(value){
  return value => 0 ? 'positive': 'negative';
}

Comments

1

try this

<input type="number" ng-class="{'positive': amount >= 0, 'negative': amount < 0}" ng-model="amount"/>

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.