2013-07-10 9 views
10

Nella vista disponibile di seguito sto tentando di selezionare un valore nella casella a discesa in base a un tasto (colorId) disponibile nell'oggetto ng-repeat corrente (utente). Qualcuno sa come farlo?Angularjs - come usare select con ng-repeat?

<div ng-app> 
    <div ng-controller="MyCntrl"> 
    <table> 
     <thead > 
       <tr> 
        <th width="50%">User</th> 
        <th width="50%" >Color</th>    
       </tr> 
      </thead> 
    <tbody> 
     <tr ng-repeat="user in users"> 
      <td width="50%">{{user.name}}</td> 
      <td width="50%"> 
       <select ng-model="user.colorid" ng-options="color.name for color in colors"> 
          <option value="">Select color</option> 
     </select> 
      </td> 
     </tr> 
     </tbody> 
    </table> 
    </div> 
</div> 

Il codice di controllo è:

'use strict'; 

angular.module('nes',) 
    .controller('MyCntrl',['$scope',function ($scope) { 
    $scope.colors = [ 
    {id:'1', name:'blue'}, 
    {id:'2', name:'white'}, 
    {id:'2', name:'red'} 
    ]; 

    $scope.users = [ 
     { name:'user1',colorId:'1'}, 
     { name:'user2',colorId:'2'} 
    ]; 
}]); 

risposta

23

devi correggere alcune cose nel vostro elemento <select>:

uso color.id as color.name nei vostri NG-opzioni.

ng-options="color.id as color.name for color in colors" 

si typoed "ColorID":

ng-model="user.colorId" 

Ecco un Plunk di farlo funzionare: http://plnkr.co/edit/DptvZuamWv9waFzI0Rcp?p=preview

+0

Grazie mille per la vostra risposta rapida! –

Problemi correlati