如何使用ng-value发送带有angularjs的隐藏字段值

这是我的forms:

<form ng-submit = "submit()"> <input ng-model="formData.data" type="text" name="sticky_content" placeholder="protein" required="true"/> <input type="hidden" name="time_start" ng-value="{{getStuff.e_time}}" required="true"/> <input ng-model="formData.end" type="text" name="time_end" placeholder="{{getStuff.e_time+100}}" required="true"/> <input type="hidden" name="zoom_level" ng-value="{{getStuff.zoom}}" required="true"/> <input type="hidden" name="latitude" ng-value="{{getStuff.lat}}" required="true"/> <input type="hidden" name="longitude" ng-value="{{getStuff.lon}}" required="true"/> <button type="submit" name="add_sticky" value="add a new stickie!">new sticky</button> </form> 

这是在我的控制器中:

 app.controller('mainCtrl',function($scope,$http,sluiceStuff){ $scope.formData={}; $scope.formData.e_time = sluiceStuff.e_time; //again, these are all filling correctly in the "inspect element tab. They just aren't submitting in the POST" $scope.formData.zoom = sluiceStuff.zoom; $scope.formData.lat = sluiceStuff.lat; $scope.formData.lon = sluiceStuff.lon; 

现在只有第一和第三个input工作 – ng模型。 我的意思是通过工作发送POST请求。 我知道,对于其他的,“{{getStuff.e_time}}”正确填充,因为我可以看到数字本身,当我检查元素。 但是,这些其他4个input甚至不提交,更不用说提交正确。 这是我的forms正确的格式,我是否正确地使用ng值? 我正在运行一个node.js服务器,但由于该请求甚至没有与所有的数据发送,我不相信在服务器上可能会有问题。

编辑:请求,这里是服务器端代码:

 app.post('/api/stickies',function(req,res){ db.run("INSERT INTO stickies (data, start, end, zoom_level, latitude, longitude) VALUES (?,?,?,?,?,?)", [ req.body.data,req.body.time_start, req.body.end, req.body.zoom_level, req.body.latitude, req.body.longitude ]); res.send(200); }); 

这里也是提交function:

 $scope.submit = function() { $scope.formData.e_time = sluiceStuff.e_time; //these 4 lines I added $scope.formData.zoom = sluiceStuff.zoom; //to make it work. But I am $scope.formData.lat = sluiceStuff.lat; //not sure that they are necessary $scope.formData.lon = sluiceStuff.lon; // or that this is the best way to do it $http.post('/node/api/stickies', $scope.formData) .then(function(data){ $scope.stickies.push(data.config.data); //console.log(data.data); },function(err){console.log(err)}); }; 

那些价值观是否需要forms? 你可以用不同的方式包含数据,也许可以通过定义一个自定义提交函数来发送这些生成的值和用户input的数据。 对我来说就像一个不必要的解决方法。 服务器期望什么? 它是一个普通的HTTP POST,还是一个API调用,期望一个JSON对象? 在后一种情况下,在提交表单之后添加额外的值将是微不足道的。

为表单命名,然后validation数据也是最好的做法。 这看起来像这样:

 <form name="myForm" ng-submit="myForm.$valid && submit()"> 

请提供更多信息,以便我们回答您的问题。 就目前来看,我认为在隐藏的投入领域是没有必要的。

对不起,这么早就回答我自己的问题,但我解决了这个问题。 但基本上我必须添加所有这些定义:

 $scope.formData.e_time = sluiceStuff.e_time; $scope.formData.zoom = sluiceStuff.zoom; $scope.formData.lat = sluiceStuff.lat; $scope.formData.lon = sluiceStuff.lon; 

到我的作用域中的submit()函数。 我想这是有道理的,因为它需要知道这些是什么,但我虽然有他们在ng值字段将保存在那里,以便他们可以在发布请求中访问。

编辑:啊所以呢,似乎我根本不需要任何隐藏的领域。 在提交时添加formData对象就足以将它们添加到发布请求中。

你应该使用ng-model指令,你可以在你的作用域中访问这个值,然后你可以把这个值作为你的post请求

 <input type="hidden" name="time_start" required="true" ng-model="getStuff.e_time"/> 

您没有正确使用ng值。 它应该没有花括号

所以你的HTML应该是:

 <form ng-submit = "submit()"> <input ng-model="formData.data" type="text" name="sticky_content" placeholder="protein" required="true"/> <input type="hidden" name="time_start" ng-value="getStuff.e_time" required="true"/> <input ng-model="formData.end" type="text" name="time_end" placeholder="{{getStuff.e_time+100}}" required="true"/> <input type="hidden" name="zoom_level" ng-value="getStuff.zoom" required="true"/> <input type="hidden" name="latitude" ng-value="getStuff.lat" required="true"/> <input type="hidden" name="longitude" ng-value="getStuff.lon" required="true"/> <button type="submit" name="add_sticky" value="add a new stickie!">new sticky</button> </form>