angularjsng-classcharactercount

Angular Conditional Class Based On Character Count


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>


Solution

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

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