【问题标题】:angularjs remove duplicate values from ngrepeatangularjs从ngrepeat中删除重复值
【发布时间】:2014-04-30 09:00:57
【问题描述】:

我正在尝试从下面的 JSON 数据中获取类别下拉列表。我有多次相同的类别,例如:计算机

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

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

【问题讨论】:

标签: angularjs


【解决方案1】:

使用

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

在这里查看更多信息:Unique & Stuff

【讨论】:

    【解决方案2】:

    您可以使用自己的函数,如下所示:

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

    【讨论】:

    • @UI_Dev :我已经在 2 个项目中使用此代码。可能它不适合你。 Omri Aharon 给出的答案是好的方法。
    猜你喜欢
    • 2013-08-17
    • 2013-04-20
    • 1970-01-01
    • 2014-03-25
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多