2016-04-30 50 views
1

我在這裏有點不知所措。我有一批隊員。每個團隊成員都有一個角色ID。我也有一系列角色。每個角色都有一個ID。我循環了團隊成員,併爲每個團隊成員顯示他們的名字和一個包含所有角色的選擇。這工作正常。我無法在選擇中預先選擇當前隊員的角色。選擇輸入中的角預選選項

這是我的數據結構:

teamMembers = [ 
       {id,100,name:John,roleId,19}, 
       {id,101,name:Sue,roleId,20}, 
       ... 
       ] 
roles = [ 
      {id:19,name:'Administrator'}, 
      {id:20,name:'Superuser'}, 
      ... 
     ] 

HTML:

<tr ng-repeat="teamMember in data.team.MEMBERS"> 
    <td> 
     {{teamMember.firstName}} {{teamMember.infix}} {{teamMember.lastName}}</td> 
<td> 
    <select 
     ng-model="teamMember.roleId" 
     ng-options="role.systemName for role in data.roles track by role.id"> 
    </select> 
</td> 

我要的是最初的選擇顯示器的作用,其中teamMember.roleId == role.id.此外,雙向數據綁定應更新teamMembr.roleId,使其始終等於當前選定的角色.id。

在Angular 1.5.0中可以這樣做嗎?如果是這樣,怎麼樣?

謝謝你的任何提示!

回答

1

嘗試這樣

var app = angular.module("testApp", []); 
 
app.controller('testCtrl', function($scope){ 
 
    $scope.open = false; 
 
$scope.teamMembers = [{id:100,name:'John',roleId:19},{id:101,name:'Sue',roleId:20}]; 
 
$scope.roles = [{id:19,name:'Administrator'},{id:20,name:'Superuser'}]; 
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="testApp" ng-controller="testCtrl"> 
 
    <table> 
 
    <tr ng-repeat="teamMember in teamMembers"> 
 
    <td> 
 
     {{teamMember.name}} </td> 
 
<td> 
 
    <select 
 
     ng-model="teamMember.roleId" 
 
     ng-options="role.id as role.name for role in roles"> 
 
    </select> 
 
</td> 
 
    </table> 
 
    
 
</div>

0
  1. 不宜role.systemNameng-optionsrole.name

  2. 使用as綁定到角色的id屬性:

    <select 
        ng-model="teamMember.roleId" 
        ng-options="role.id as role.name for role in data.roles track by role.id"> 
    </select>