javascript - 如何使用 ng-value 通过 angularjs 发送隐藏字段值

标签 javascript node.js angularjs

这是我的表格:

<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>

这是在我的 Controller 中:

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;

现在只有第一个和第三个输入有效 - ng-model 输入。我所说的工作是指通过 POST 请求发送。我知道对于其他的,“{{getStuff.e_time}}”填充正确,因为当我检查元素时我可以看到数字本身。然而,这另外4个输入根本就没有提交,更不用说正确提交了。这是我的表单的正确格式吗?我是否正确使用了 ng-value ?我正在运行一个 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);
});

这里还有提交函数:

$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)});
    };

最佳答案

这些值需要采用表单形式吗?您能否以不同的方式包含数据,例如定义一个自定义提交函数,将这些生成的值与用户输入的数据一起发送?在我看来这是一个不必要的解决方法。服务器期望什么?它是常规 HTTP POST,还是需要 JSON 对象的 API 调用?在后一种情况下,在提交表单后添加额外的值将很简单。

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

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

请提供更多信息,以便我们更轻松地回答您的问题。就目前情况而言,我认为没有必要将这些字段隐藏在输入中。

关于javascript - 如何使用 ng-value 通过 angularjs 发送隐藏字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25294877/

相关文章:

javascript - AJAX 与 History.pushState

javascript - 使用 javascript/jquery 更改/添加 div 属性

javascript - 如何从 JSON 数组按月排序对象?

google-chrome-extension - 在 Google Chrome 扩展中使用 AngularJS

html - 我应该使用哪个类来解决 angularjs 输入验证的问题?

php - JavaScript/PHP 在电子邮件提交按钮后没有响应

javascript - 根据某个值进行 x 次计算

node.js - 在 Node.js 中保存大型文档时,MongoDB-mongoose 高 CPU 使用率

javascript - Webpack 将 scss 编译为 css 并缩小

javascript - firebase.analytics 不是函数