[英]AngularJS 'PUT' requests. How to submit ONLY the data in form?
I'm trying to update user data, but when I make the 'PUT' request, Angular is sending the entire user $scope rather than just the fields exposed by the form. 我正在尝试更新用户数据,但是当我发出“ PUT”请求时,Angular将发送整个用户$ scope,而不仅仅是发送表单公开的字段。
I'm using a Factory to get and put the data. 我正在使用工厂来获取和放置数据。 Here's my edit function 这是我的编辑功能
$scope.editStudent = function () {
Student.edit ({id: $stateParams.studentId}, $scope.Student, function (data) {
$location.path('/');
});
};
This doesn't work for me because the server I'm sending to does it's own server-side validation, and if I'm sending the entire student scope there will be some fields that are blank, thus will not pass validation. 这对我来说不起作用,因为我要发送的服务器是它自己的服务器端验证,并且如果我要发送整个学生范围,则某些字段将为空白,因此不会通过验证。
The server allows me to send just the field I need to update, so I'm looking for an Angular way of doing this. 服务器允许我仅发送需要更新的字段,因此我正在寻找一种实现此目的的Angular方法。
Here's a screenshot to help explain my question: 这是一个截图,可以帮助解释我的问题:
You can write function to check the field defined in the form and then only submit the value. 您可以编写函数来检查表单中定义的字段,然后仅提交值。 I think the code is self explanatory. 我认为代码是不言自明的。
Code Snippet 代码段
app.controller('MainCtrl', function($scope, $http) {
$scope.user = {
"name": "Ali",
"company": "MSB",
getData: function(form) {
var data = {};
angular.forEach(this, function(fieldValue, fieldName) {
if (!angular.isFunction(fieldValue) && form.$isSubmit(fieldName)) {
data[fieldName] = fieldValue;
}
});
return data;
}
}
$scope.save = function() {
$scope.userForm.$isSubmit = function(fieldName) {
return !angular.isUndefined((this[fieldName]));
}
console.log($scope.user.getData($scope.userForm));
$scope.data = $scope.user.getData($scope.userForm);
//$http.put('url', $scope.data)
}
});
I created the plunkr - http://plnkr.co/edit/6g5myBvBmWZ2IvKMLuvL?p=preview 我创建了plunkr- http: //plnkr.co/edit/6g5myBvBmWZ2IvKMLuvL?p=preview
*Tips (or rules) *提示(或规则)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.