我正在尝试填充表单以编辑用户列表。
的index.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>
controllers.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,Rene
答案 0 :(得分:2)
以下解决方案有效,但我认为它不是很优雅:
<强>的index.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>
<强> controller.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;
}
}]);