0

我尝试用每行选择一个表格。表中的数据和选择字段来自数组...

<div ng-app="app" ng-controller="MainCtrl">
<table>
    <thead>
        <tr>
            <th>Headline #1</th>
            <th>Headline #2</th>
            <th>Headline #3</th>
            <th>Headline #4</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="user in users">
            <td>{{user.id}}</td>
            <td>{{user.name}}</td>
            <td>
                <select name="" id="">
                    <option value="" ng-repeat="role in roles">{{role.name}}</option>
                </select>
            </td>
            <td>anything...</td>
        </tr>
    </tbody>    
</table>

angular.module('app', [])
.controller('MainCtrl', function ($scope) {

        $scope.users = [
            {
                id : 1,
                name : 'Andrew',
                role : "admin"
            },
            {
                id : 2,
                name : 'Tanya',
                role : "admin"
            },
            {
                id : 3,
                name : 'Roland',
                role : "author"
            },
        ];

        $scope.roles = [
            {
                name : "author"
            },
            {
                name : "admin"
            }

        ];



});

在这里看我的例子: jsFiddle

我需要的很简单,每个用户都有一个角色,我希望每一行都选择正确的角色......

对不起我的英语很糟糕......

谢谢内森

4

1 回答 1

0

id像这样为角色数据列表添加字段

$scope.roles = [{
    id: "author",
    name: "author"
}, {
    id: "admin",
    name: "admin"
}];

并将select元素更改为

<select ng-model="user.role" ng-options="role.id as role.name for role in roles"></select>

Demo on jsFiddle

于 2013-07-30T22:29:10.660 回答