Daveus
Daveus

Reputation: 121

Angular js ng-repeat duplicate items

I have little problem with my ng-repeat

  $http.get("/test-server/rest/servizi/listaRuoli")
    .success(function(data){
    	$scope.servizio = data;
    	console.log(data.label);
    })
 <div class ="input_form_right">
    <strong>Seleziona un Servizio</strong> (obbligatorio)<br>
    <select class="size_input_newbg">
        <option ng-repeat="x in servizio" ng-bind = "x.label"></option>
    </select>
 </div>

the problem is that I can't use track by $index because of mongodb as I read on the web. Some ideas? thank you!

Upvotes: 3

Views: 264

Answers (2)

Xarus
Xarus

Reputation: 7019

Try this:

 <style>
      .title-case {
            text-transform: capitalize;
      }
 </style>

 <div class ="input_form_right">
      <strong>Seleziona un Servizio</strong> (obbligatorio)<br>
      <select class="size_input_newbg">
           <option class="title-case" ng-repeat="x in servizio track by $index">{{x.label}}</option>
      </select>
 </div>

MongoDB has nothing to do with tracking by $index. If you are trying to remove all duplicate options, I'd recommend changing your GET call to:

$http.get("/test-server/rest/servizi/listaRuoli")
.success(function(data){
    var seenBefore = [];
    var out = [];
    for (var i=0;i<=data.length-1;i++){
         if (seenBefore.indexOf(data[i].label) == -1) {
              seenBefore.push(data[i].label);
              data[i].label = data[i].label.toLowerCase();
              out.push(data[i]);
          }
    }
    $scope.servizio = out;
    console.log(data.label);
})

Upvotes: 0

Thalaivar
Thalaivar

Reputation: 23632

Use ng-options instead:

<select  class="size_input_newbg" ng-model="yourmodel" 
  ng-options="r.id as r.label for r in servizio"
  ng-bind = "r.label">
   <option value="" disabled="">Select One</option>
</select>

Upvotes: 2

Related Questions