要编辑用户信息,当我更改用户角色并提交表单时,会将旧的“ id”传递给服务,但是,当我更改角色时,它表明id已更改(我将其打印在屏幕
选择的值是:{{selectedItemvalue}}
)。但是仍然传递旧的“ id”,例如,如果用户角色是ID为2的admin,并且如果我将其更改为ID为1的用户,则应该传递角色ID 1而不是2,但传递的是旧ID 2。我已经检查了下面的帖子,但没有成功。AngularJS: Get selected Item
<div class="col-md-7">
<select ng-model="selectedItemvalue">
<option ng-repeat="role in roless" ng- selected="selectedItemvalue == role.id"
value="{{role.id}}">{{role.name}}</option>
</select>
<p>Selected Value is : {{selectedItemvalue}}</p>
</div>
<form ng-submit="submitUserForm(user.id)" name="myForm"
class="form-horizontal">
app.controller('usersDetailsController',函数($ rootScope,$ scope,$ http,
$location,$routeParams,$route) {
if ($rootScope.authenticated && $rootScope.role=="ADMIN") {
$scope.roless = [{"id": 1, "name": "USER"}, {"id": 2, "name":"ADMIN"}];
$scope.userId = $routeParams.id;
$http({
method: 'GET',
url: 'http://localhost:8080/api/user/' + $scope.userId
}).then(function (response) {
$scope.user = response.data;
$scope.selectedItemvalue = $scope.user.roles[0].id;
console.log("CurrentRole..."+ $scope.selectedItemvalue);
console.log("Current data of user..."+ JSON.stringify(response.data));});
$scope.submitUserForm = function (userId) {
$http({
method: 'PUT',
url: 'http://localhost:8080/api/user/' + userId,
data: $scope.user
}).then(
function (response) {
console.log("Updated Role data..."+ JSON.stringify(response.data));
$location.path("/list-all-users");
$route.reload();
},
function (errResponse) {
$scope.errorMessage = "Error while updating User - Error Message: '" + errResponse.data.errorMessage;
});
}
这里的问题是数据类型。此表达式value="{{role.id}}"
指向string
数据类型。因此,请按如下所示分配用户ID,
$scope.selectedItemvalue = ''+$scope.user.roles[0].id;
在您的代码中,您可以检查$ scope.selectedItemvalue = $ scope.user.roles [0] .id;。因此,您将第0个角色设置为$ scope.selectedItemvalue,并且覆盖了最新值。
最后找到问题所在,并通过向$ scope.submitUserForm添加以下代码行来解决问题...实际上,当您提交表单时,它需要从ng-model获取ID并将其设置为用户的角色ID,例如这个...$ scope.user.roles [0] .id = $ scope.selectedItemvalue;