vzupo vzupo - 5 months ago 20
AngularJS Question

angularjs check if ng-model != isFinite

I have the word infinity appear in my output and I am trying to change its color when it becomes and actual number.

So when I check to see if it isFinite it changes to orange, but then when it actually becomes a number- I cant get it to change to black. I am so close with this, am I writing this wrong?

<span ng-model="users2" ng-class="{'test3':users2 === isFinite , 'test2':users2 !== isFinite}" ng-hide="!myVar" style="font-size:28px; color:purple" >

Answer

Am I writing this wrong?

Yes... several issues

isFinite is function first off. Also it is in global namespace whereas angular expressions are evaluated against scope variables

You would need to define isFinite(testValue) in angular scope (controller) to use it in the view

// be sure to inject $window in controller
$scope.isFinite = $window.isFinite

And in view would be:

ng-class="{'test3':isFinite(users2) , 'test2':!isFinite(users2)}"

Reference MDN isFinite()

Comments