2014-04-30 156 views
2

我想從下面的JSON數據中獲取一個類別下拉列表。我有相同類型的多個次,例如:電腦angularjs從ngrepeat中刪除重複的值

[ 
{ 
"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> 

我想只有一次顯示重複的類別。請給我一些建議,如何達到要求的輸出。提前致謝。

+1

重複http://stackoverflow.com/questions/15914658/angular-js-how-to-make-ng-repeat-filter-out-duplicate-results –

回答

1

使用

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

在這裏尋找更多:Unique & Stuff

1

您可以使用自己喜歡的功能如下:

<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); 
} 

});

+0

請在您的答案中格式化您的代碼。 –