【发布时间】:2016-02-03 23:15:15
【问题描述】:
如果该对象的属性(名称)存在于另一个数组中,我正在尝试向 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>
【问题讨论】:
标签: javascript arrays angularjs ng-repeat