Search code examples
htmlangularjsangularjs-ng-repeatangular-filters

Remove ng-repeat duplicates


I have already read about this but didn't understand how to do this kind of things.

<div class='dropdown2'>
     <span>Seleziona Campionato:      </span>
     <select class='opzioni' ng-model="campionatoSelected">
              <option ng-repeat="team in teams  | filter: {Paese:nazioniSelected} track by $index" value="{{team.Campionato}}">{{team.Campionato}}
             </option>
     </select>
</div>

This is the piece of code that gives me duplicates, cause in every league (campionato) there are many teams. How can i cut the duplicates off this select?


Solution

  • You can use the unique filter provided by the module angular-filter (https://github.com/a8m/angular-filter) and cut off all the duplicates using the property you want to use for filtering:

    <select class='opzioni' ng-model="campionatoSelected">
      <option ng-repeat="team in teams | filter: {Paese:nazioniSelected} | unique: 'MY_PROPERTY_NAME' track by $index" value="{{team.Campionato}}">{{team.Campionato}}
      </option>
    </select>
    

    You can also define your private filter and use that for doing both the operations you want to without adding third party modules.

    PS: I am Italian like you, so I got your point, but try to use English variable/function names, it would make the code so much more readable, especially if then you want to post it somewhere or share it in general.