Search code examples
javascriptangularjsfilterng-class

AngularJS ng-class filter not working


I have a simple filter on my ng-class that looks for a rid that begins with '1A' in my ng-repeat of service in services

ng-class="{'couldBeWrong':rid.indexOf('1A') > -1}"

If the rid begins with 1A then the class of couldBeWrong should be applied

I have also tried various variations such as

class="{'couldBeWrong':rid.indexOf('1A') > -1}"

and

ng-class="{'couldBeWrong':service.rid.indexOf('1A') > -1}"

EDIT : Based on accepted answer, here is my solution

Added to controller

$scope.getClass = function(field){
    if(field.indexOf('1L') > -1){
        return true;//or you can modify as per your need
    }else{
        return false;//you can modify as per your need   
    }
}

and the NG-CLASS in the repeat

ng-class="{'couldBeWrong':getClass(service.rid)}"

Solution

  • Try this

    ng-class="{'couldBeWrong':getClass(rid)}"
    

    In Controller

    $scope.getClass = function(){
      if(rid.indexOf('1A') > -1){
        return true;//or you can modify as per your need
      }else{
        return false;//you can modify as per your need   
      }
    }