【问题标题】:AngularJS ng-class filter not workingAngularJS ng-class过滤器不起作用
【发布时间】:2017-02-24 12:31:12
【问题描述】:

我在我的 ng-class 上有一个简单的过滤器,它在我的服务中的 ng-repeat 中寻找以“1A”开头的摆脱

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

如果 rid 以 1A 开头,则应应用 canBeWrong 类

我也尝试过各种变体,例如

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

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

编辑:根据接受的答案,这是我的解决方案

添加到控制器

$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   
    }
}

和重复中的NG-CLASS

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

【问题讨论】:

    标签: javascript angularjs filter ng-class


    【解决方案1】:

    试试这个

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

    在控制器中

    $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   
      }
    }
    

    【讨论】:

    • 您忘记关闭括号:ng-class="{'couldBeWrong':getClass(rid)}"
    • 是的 @VitaliyAndrusishyn 因为我没有运行它:)
    • 我的答案基于此,只是稍微调整了一下......现在添加到 OP
    【解决方案2】:

    您似乎忘记了右括号。应该是这样的:

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

    【讨论】:

    • 能否提供模板加控制器。根据您提供的内容,它应该可以工作
    猜你喜欢
    • 1970-01-01
    • 2016-02-06
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    相关资源
    最近更新 更多