在下面的视图中,我试图根据当前ng-repeat对象(用户)中可用的键(colorId)在下拉框中选择一个值。有谁知道怎么做?
<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>
控制器代码为:
'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'}
];
}]);
答案 0 :(得分:23)
您需要在<select>
元素中修复一些内容:
在你的ng-options中使用color.id as color.name
。
ng-options="color.id as color.name for color in colors"
你打错了“colorid”:
ng-model="user.colorId"
这是一个有效的工作:http://plnkr.co/edit/DptvZuamWv9waFzI0Rcp?p=preview