如何使纳克重复过滤掉重复的结果 [英] How to make ng-repeat filter out duplicate results

查看:128
本文介绍了如何使纳克重复过滤掉重复的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行一个简单的 NG-重复在一个JSON文件,并希望得到类别名称。大约有100个对象,每个属于一个类别 - 但只有大约6大类。

I'm running a simple ng-repeat over a JSON file and want to get category names. There are about 100 objects, each belonging to a category - but there are only about 6 categories.

我目前的code是这样的:

My current code is this:

<select ng-model="orderProp" >
  <option ng-repeat="place in places" value="{{place.category}}">{{place.category}}</option>
</select>

输出为100不同的选择,主要是重复的。如何使用角度来检查是否 {{} place.category} 已经存在,并没有营造一个选项,如果它已经存在?

The output is 100 different options, mostly duplicates. How do I use Angular to check whether a {{place.category}} already exists, and not create an option if it's already there?

编辑:在我的javascript, $ scope.places = JSON数据,我只想澄清

edit: In my javascript, $scope.places = JSON data, just to clarify

推荐答案

您可以使用唯一过滤器从AngularUI(来源$ C ​​$ C可在这里:<一href=\"https://github.com/angular-ui/angular-ui/blob/master/modules/filters/unique/unique.js\">AngularUI独特的过滤),并直接在NG-选择使用它(或NG-重复)。

You could use the unique filter from AngularUI (source code available here: AngularUI unique filter) and use it directly in the ng-options (or ng-repeat).

<select ng-model="orderProp" ng-options="place.category for place in places | unique:'category'">
    <option value="0">Default</option>
    // unique options from the categories
</select>

这篇关于如何使纳克重复过滤掉重复的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆