Angular Conditional Class Based On Character Count

809 Views Asked by At

How to conditionally apply a class based on the character count of the model?

E.g.:

$scope.sample = 555;

<span ng-class="{ 'char3': sample.length == 3 }">{{ sample }}</span>

2

There are 2 best solutions below

0
On BEST ANSWER

You can convert sample to string and the check its length:

<span ng-class="{ char3: (sample + '').length == 3 }">{{ sample }}</span>
0
On
<span ng-class="(sample.length === 3) ? 'three':'notthree'">{{ sample }}</span>