Calling functions in ngClass

Asked

Viewed 415 times

0

I’d like to call in functions in the ngClass. I was able to call only the function, and if I want to call other classes it does not take, if I just put the function in the class it can call(ng-class="styleClass(5)"), but I want some classes to always be and others can be changed by other validations. When I take the function from the example, it works all right.

Follow the Example:

Javascript

$scope.styleClass = function(i) {
  console.log("entrou");
  if(i>1){
      return 'bg';
  }
  return ''
}

HTML

First Name: <input type="text" ng-class="{'text':true, 'padd':true, 'styleClass(5)':true}" ng-model="firstName"><br>
  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

2 answers

1


I talked to some friends and found a way, which is

ng-class="[styleClass(5),{'text': true, 'padd': true}]"
  • It probably works that way too ng-class="[styleClass(5), 'text', 'padd']"

  • 1

    yes, it works but so can not put conditionals if necessary

0

You can mix the attribute class (with fixed classes) with the ng-class for function return:

<input type="text" class="text padd" ng-class="styleClass(5)" ng-model="firstName">
  • is that I have to do other validations of other classes in ng-class

  • 1

    I talked to some friends of mine and found a way, which is ng-class="[styleClass(5),{'text': true, 'padd': true}]"

  • @Eduardobobato if you want you can insert this as another answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.