我进行了 $http.post
调用,但我的隐藏字段未随表单一起发布。
代码:
<sf:form ng-submit="post(form)" id="respond" >
<input type="hidden"name="form.replyTo" ng-model="replyTo" ng-value="1">
<input type="hidden" name="form.id" ng-model="id" ng-value="33" >
<input class="diskafield" type="text" ng-model="form.name" >
<input class="diskafield" type="text" ng-model="form.email" >
<textarea class="diskafield" name="comments" ng-model="form.body" required=""></textarea>
<input class="diskabtn" type="button" ng-click="post(form)" value="Post Comment">
</div>
</sf:form>
我的 Controller 是:
app.controller('AddCommentController', function ($scope, $http) {
$scope.name="";
$scope.email="";
$scope.body="";
$scope.id = "";
$scope.replyTo = '';
$scope.post= function(form) {
$http.post('http://localhost:8080/add', form).success(function(response){
}).error(function(response){
console.log(response);
})
}
});
最佳答案
您的发布 form
对象,因此,如果您需要将隐藏属性附加到该 form
对象,
<input type="hidden"name="form.replyTo" ng-model="form.replyTo" ng-value="1">
<input type="hidden" name="form.id" ng-model="form.id" ng-value="33" >
您需要将模型更改为 form.replyTo
和 form.id
,如上所示。
似乎您错过了使用ng-model
和name
,
例如:
你提到$scope.name
并且您在表单中将其用作元素名称。
这不是使用它的方式,这里范围变量和输入名称之间没有关系。
这应该与ng-model
相关。
如此干净的例子应该是这样的。
$scope.form = {
name : "",
email : "",
body : "",
id : 33,
replyTo : 1
};
$scope.post= function(form) {
$http.post('http://localhost:8080/add', form).success(function(response){
}).error(function(response){
console.log(response);
})
}
<sf:form ng-submit="post(form)" id="respond" >
<input type="hidden"name="form.replyTo" ng-model="form.replyTo">
<input type="hidden" name="form.id" ng-model="form.id" >
<input class="diskafield" type="text" ng-model="form.name" >
<input class="diskafield" type="text" ng-model="form.email" >
<textarea class="diskafield" name="comments" ng-model="form.body" required=""></textarea>
<input class="diskabtn" type="button" ng-click="post(form)" value="Post Comment">
</div>
</sf:form>
实际上您可以删除两个隐藏字段,因为隐藏字段数据存在于 $scope.form
对象中。
这是一个工作DEMO
关于javascript - Angularjs 不发布隐藏值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31054100/