如何使用ng click AngularJS直接更新字段
我试图添加一个函数:通过单击按钮在用户界面上更改字段值。具体来说,当我单击名为如何使用ng click AngularJS直接更新字段,angularjs,spring-boot,Angularjs,Spring Boot,我试图添加一个函数:通过单击按钮在用户界面上更改字段值。具体来说,当我单击名为删除的按钮时,我想将状态字段从默认值true更改为false 我想用ng click调用函数控制器Spring和PUT方法 这是我的密码: 1.重启控制器 @RequestMapping(value = "/user/{id}/delete", method = RequestMethod.PUT) public ResponseEntity<?> changeUserStatus(@PathVa
删除的按钮时,我想将状态
字段从默认值true
更改为false
我想用ng click调用函数控制器Spring和PUT方法
这是我的密码:
1.重启控制器
@RequestMapping(value = "/user/{id}/delete", method = RequestMethod.PUT)
public ResponseEntity<?> changeUserStatus(@PathVariable("id") long id, @RequestBody User user){
logger.info("Change User with id {}", id);
User currentUser = userService.findById(id);
if (currentUser == null){
logger.error("Unable to change. User with id {} not found.", id);
return new ResponseEntity(new CustomErrorType("Unable to change. User with id " + id + " not found."),
HttpStatus.NOT_FOUND);
}
currentUser.setStatus(false);
userService.changeUserStatus(currentUser);
return new ResponseEntity<User>(currentUser, HttpStatus.OK);
}
用户控制器
字尾
用户列表
身份证件
名称
年龄
薪水
地位
{{u.id}}
{{u.name}
{{u.age}
{{u.salary}}
{{u.status}}
删除
当我运行应用程序并单击“删除”时,它抛出以下错误:`.w.s.m.s.DefaultHandlerExceptionResolver:绑定请求元素失败:org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:未能将类型“java.lang.String”的值转换为所需的类型“long”;嵌套异常为java.lang.NumberFormatException:对于输入字符串:“未定义”`
尝试更改
$http.put(urls.USER_SERVICE_API + id + "/delete" , user)
到
您的端点是yourapi/user/userId/delete
,而不是yourapi/userId/delete
,这一行位于您的服务控制台.log('使用id+id更改用户状态')代码>记录正确的id??我无法获取id,现在是未定义的值,这是问题所在。您需要确保您正在从前端传递正确的id。试着调试你的代码,看看为什么id是未定义的。谢谢,我会试试的
var self = this;
function changeUserStatus(user, id){
console.log('About to change user status');
UserService.changeUserStatus(user, id)
.then(
function (response){
console.log('User updated successfully');
self.successMessage='User updated successfully';
self.errorMessage='';
self.done = true;
$scope.myForm.$setPristine();
},
function(errResponse){
console.error('Error while updating User');
self.errorMessage='Error while updating User '+errResponse.data;
self.successMessage='';
}
);
}
<div class="panel-heading"><span class="lead">List of Users </span></div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>AGE</th>
<th>SALARY</th>
<th>STATUS</th>
<th width="100"></th>
<th width="100"></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="u in ctrl.getAllUsers()">
<td>{{u.id}}</td>
<td>{{u.name}}</td>
<td>{{u.age}}</td>
<td>{{u.salary}}</td>
<td>{{u.status}}</td>
<td><button type="button" ng-click="ctrl.changeUserStatus(u.id)" class="btn btn-default custom-width">Delete</button></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
When I run application and click on `Delete`, it throw this error: `.w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException: For input string: "undefined"`
$http.put(urls.USER_SERVICE_API + id + "/delete" , user)
$http.put(urls.USER_SERVICE_API + "/" + id + "/delete" , user)