基于另一个数组,以角度为基础添加类到ng-repeat对象

时间:2016-02-03 14:50:13

标签: javascript arrays angularjs ng-repeat

如果该对象(name)的属性存在于另一个数组中,我试图向ng-repeat中的对象添加一个类。

基本上,用户可以将ng-repeat中的对象标记为正确或不正确,从而创建"判断"判断数组中的对象。在页面上载时,我希望能够比较两个数组,根据对象的最近判断是否错误或正确来添加/删除一个类。

根据我的小提琴,item1和item3应该有一个"不正确"的类。我怎么能做到这一点?

我尝试使用inArray(请参阅http://jsfiddle.net/arunpjohny/wnnWu/),但无法弄清楚如何使用特定属性而不是整个数组。

小提琴:http://jsfiddle.net/bTyAa/2/

function itemCtrl($scope) {
  $scope.items = [{
    itemname: "item1"
  }, {
    itemname: "item2"
  }, {
    itemname: "item3"
  }];
  $scope.judgements = [{
    judgementResult: "incorrect",
    date: "2016-02-01T11:03:16-0500",
    item: {
      itemname: "item1"
    }
  }, {
    judgementResult: "correct",
    date: "2016-01-06T11:03:16-0500",
    item: {
      itemname: "item1"
    }
  }, {
    judgementResult: "incorrect",
    date: "2016-01-04T11:03:16-0500",
    item: {
      itemname: "item3"
    }
  }]
}


<div ng-app>
  <div ng-controller="itemCtrl">
    <ul>
      <li ng-repeat="item in items" class="item"> <span>{{item.itemname}}</span>

      </li>
    </ul>
  </div>
</div>

3 个答案:

答案 0 :(得分:2)

尝试定义新功能以获得适合您项目的课程

this SO post

$scope.getJudgementsClass = function(itemName) {
    var matched = $scope.judgements.filter(function(el) {
        return el.item.itemname === itemName; 
    }).sort(function(a,b){
        // Turn your strings into dates, and then subtract them
        // to get a value that is either negative, positive, or zero.
        return new Date(b.date) - new Date(a.date);
        });
    if (matched.length == 0)
    {
        return "";
    }
    console.log(itemName);
    return matched[0].judgementResult;
}

答案 1 :(得分:0)

为什么不在第二个数组上进行NG重复,因为你已经将第一个数组中的信息作为第二个数组中的项目。

并且只使用判断结果作为类?

&#13;
&#13;
function itemCtrl($scope) {
  $scope.items = [{
    itemname: "item1"
  }, {
    itemname: "item2"
  }, {
    itemname: "item3"
  }];
  $scope.judgements = [{
    judgementResult: "incorrect",
    date: "2016-02-01T11:03:16-0500",
    item: {
      itemname: "item1"
    }
  }, {
    judgementResult: "correct",
    date: "2016-01-06T11:03:16-0500",
    item: {
      itemname: "item1"
    }
  }, {
    judgementResult: "incorrect",
    date: "2016-01-04T11:03:16-0500",
    item: {
      itemname: "item3"
    }
  }]
}
&#13;
<div ng-app>
  <div ng-controller="itemCtrl">
    <ul>
      <li ng-repeat="obj in judgements" ng-class="obj.judgementResult"> <span>{{obj.item.itemname}}</span>

      </li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您无法在每次$scope.judgements次迭代中遍历ng-repeat数组。类似的东西:

$scope.isItemInCorrect =  function(itemname){
    console.log($scope.judgements);
    for(var i = $scope.judgements.length; i--;) {
      if ($scope.judgements[i].item.itemname == itemname 
          && $scope.judgements[i].judgementResult == 'incorrect') {
          return true;
      } 
    }        
}

<li ng-repeat="item in items" ng-class="{'incorrect' : isItemInCorrect(item.itemname)}"  class="item"> <span>{{item.itemname}}</span>

http://jsfiddle.net/n0eb82j3/7/