How to make ng-repeat filter out duplicate results

前端 未结 16 2933
鱼传尺愫
鱼传尺愫 2020-11-22 06:52

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

16条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 07:21

    You can use 'unique'(aliases: uniq) filter in angular.filter module

    usage: colection | uniq: 'property'
    you can also filter by nested properties: colection | uniq: 'property.nested_property'

    What you can do, is something like that..

    function MainController ($scope) {
     $scope.orders = [
      { id:1, customer: { name: 'foo', id: 10 } },
      { id:2, customer: { name: 'bar', id: 20 } },
      { id:3, customer: { name: 'foo', id: 10 } },
      { id:4, customer: { name: 'bar', id: 20 } },
      { id:5, customer: { name: 'baz', id: 30 } },
     ];
    }
    

    HTML: We filter by customer id, i.e remove duplicate customers

    Customer list: 
    
        {{ order.customer.name }} , {{ order.customer.id }} 
    
    

    result
    Customer list:
    foo 10
    bar 20
    baz 30

提交回复
热议问题