angularjs从ngrepeat中删除重复值

时间:2014-04-30 09:00:57

标签: angularjs

我正在尝试从JSON数据下面获取一个类别下拉列表。我有多次相同的类别,例如:Ex:Computer

[
{ 
"title":"C Programming", 
"category":"Computer"
},
{ 
"title":"International Tax", 
"category":"Business"
},
{ 
"title":".net Programming", 
"category":"Computer"
}
//more data...
]

AngularJS:

function showSubjects($scope, $http)
{
$http({method: 'POST', url: 'js/subjects.json'}).success(function(data)
{
$scope.items= data; // response data 
});
}

HTML:

<div id="ng-app" ng-app ng-controller="showSubjects">
<select>
<option ng-repeat="subjects in items">{{subjects.category}}</option>
</select>
</div>

我想只显示一次重复的类别。请给我一些建议,如何实现所需的输出。提前谢谢。

2 个答案:

答案 0 :(得分:2)

使用

<option ng-repeat="subjects in items | unique:'category'">{{subjects.category}}</option>

在此处查看更多信息:Unique & Stuff

答案 1 :(得分:0)

您可以使用自己的功能,如下所示:

   <select name="cmpPro" ng-model="test3.Product" ng-options="q for q in productArray track by q">
    <option value="" >Plans</option>
</select>

productArray =[];
angular.forEach($scope.leadDetail, function(value,key){
var index = $scope.productArray.indexOf(value.Product);
if(index === -1)
{
    $scope.productArray.push(value.Product);
}
});