gpt4 book ai didi

Angularjs - 如何使用带有 ng-repeat 的选择?

转载 作者:行者123 更新时间:2023-12-04 01:38:11 25 4
gpt4 key购买 nike

在下面可用的 View 中,我试图根据当前 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>

Controller 代码是:
'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'}
];
}]);

最佳答案

您需要在 <select> 中修复一些问题元素:

使用 color.id as color.name在你的 ng-options 中。

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

你输入了“colorid”:
ng-model="user.colorId"

这是它的一部分工作: http://plnkr.co/edit/DptvZuamWv9waFzI0Rcp?p=preview

关于Angularjs - 如何使用带有 ng-repeat 的选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17574081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com