0

我正在尝试填充表单以编辑用户列表。

索引.html:

<h1>Users</h1>
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Role</th>
        </tr>
    </thead>
    <tbody>
        <tr data-ng-repeat="u in users" data-ng-click="select(u)">
            <td>{{u.id}}</td>
            <td>{{u.username}}</td>
            <td>{{u.role.title}}</td>
        </tr>
    </tbody>
</table>
<form>
    <label for="inputUsername">Username</label>
    <input data-ng-model="selectedUser.username" id="inputUsername" type="text">
    <select  data-ng-model="selectedUser.role"
             data-ng-options="r as r.title for r in roles"
             data-ng-change="">
    </select>
</form>

<pre>{{selectedUser |json}}</pre>

控制器.js:

angular.module('test-select')
        .controller('IndexCtrl',
        ['$scope', 'Restangular', function($scope, Restangular) {
                $scope.selectedUser = null;

                Restangular.all("roles").getList().then(function(roles) {
                    $scope.roles = roles;
                });
                Restangular.all("users").getList().then(function(users) {
                    $scope.users = users;
                });

                $scope.select = function(user) {
                    $scope.selectedUser = user;
                };
            }]);

我想要实现的是,当我单击表格上的一行时,我可以在表单中编辑选定的用户(以保存新值),除了用户角色之外,一切正常。如果我单击一行,则不会更新所选用户角色。

我认为这与通过 Restangular 调用的角色列表有关。有谁知道如何优雅地解决这个问题?

PS:我使用的是角度 1.2-rc2。

BR,雷内

4

1 回答 1

2

以下解决方案有效,但我认为它不是很优雅:

索引.html

<h1>Users</h1>

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Role</th>
        </tr>
    </thead>
    <tbody>
        <tr data-ng-repeat="u in users" data-ng-click="select(u)">
            <td>{{u.id}}</td>
            <td>{{u.username}}</td>
            <td>{{u.role.title}}</td>
        </tr>
    </tbody>
</table>

<form>
    <label for="inputUsername">Username</label>
    <input data-ng-model="selectedUser.username" id="inputUsername" type="text">
    <select  data-ng-model="selectedUser.role.id"
             data-ng-options="r.id as r.title for r in roles"
             data-ng-change="update()">
    </select>
</form>

控制器.js

'use strict';

/* Controllers */

angular.module('test-select')
        .controller('IndexCtrl',
        ['$scope', 'Restangular', '$resource', function($scope, Restangular, $resource) {
                $scope.selectedUser = null;

                Restangular.all("roles").getList().then(function(roles) {
                    $scope.roles = roles;
                });
                Restangular.all("users").getList().then(function(users) {
                    $scope.users = users;
                });



                $scope.select = function(user) {
                    $scope.selectedUser = user;
                };

                $scope.update = function() {
                     var role = _.find($scope.roles, function(role){
                         return role.id === $scope.selectedUser.role.id;
                     });
                     $scope.selectedUser.role.title = role.title;
                     $scope.selectedUser.role.bitMask = role.bitMask;
                }
            }]);
于 2013-10-09T11:48:16.307 回答