添加值数组并使用ng-class突出显示数组中的所有值

时间:2016-08-04 12:28:53

标签: css angularjs

我有2 ng-repeat,在选择内部索引时,外部索引应该突出显示。我需要为突出显示的值添加颜色,我使用多个值(即值数组)     点击内部ng-repeat我试图将值推到范围,

              $scope.setData=function(isSelected) {
                 $scope.isSelected.push(isSelected);
                    console.log(isSelected+" "+$scope.isSelected);
                         }
              <div class="row>"
              <div ng-repeat="out in outs track by $index" >  
                    <a href=""  
                    ng-class="getClass($index)">{{out.label}}</a></div></div>
                    <!-- </div> -->
                </div>

               <div ng-repeat="out in outs track by $index" >
               <div ng-if="out_index== $index">
               <div>{{out.label}}</div>
               <div class="col-md-8">
                                                   <!--  <div class="jumbotron vcenter">  --> 
                                                   <ul class="list-group" ng-repeat="inner in out.inners | orderBy: 'id' " >
                                                        <li class="list-group-item cursorPointer">
                                                             <img src="{{inner.location}}" alt="{{inner.description}}" title="{{inner.description}}"   ng-dblclick="setData($parent.$index)"/>
                                                             <div><font >{{inner.description}}</font></div>
                                                            </li></ul>  
                                                        </div>                                                                  
                                                    <!-- </div>   -->
                                                </div>
                                            </div>      

 $scope.isSelected = [];
 $scope.getClass = function(ind){
     for(var i=0;i<$scope.isSelected.length;i++){
    console.log("Length "+$scope.isSelected.length+" Scope >>"+$scope.isSelected[i]+" Index "+ind);
    if( $scope.isSelected[i] ==ind){
        return "selected";
    } else{
        return "";
    }
  }
}

2 个答案:

答案 0 :(得分:0)

 if( $scope.isSelected.indexOf(ind) !== -1){
        return "selected";
    } else{
        return "";
    }

它对我有用

答案 1 :(得分:0)

For循环在第一次迭代时终止。循环后删除elsereturn ""

 $scope.getClass = function (ind) {
     for (var i = 0; i < $scope.isSelected.length; i++) {
          if ($scope.isSelected[i] == ind) {
                return "selected";
          } 
      }
      return "";
}