ExpressJS AngularJS POST [英] ExpressJS AngularJS POST

查看:21
本文介绍了ExpressJS AngularJS POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 AngularJS,我想知道如何使用 ExpressJS 将它与 Node 正确连接.

I'm learning AngularJS and I want to know how to correctly wire it with Node with ExpressJS.

这是我的控制器:

app.controller('view1Ctrl', function($scope, $http) {

    $scope.sub = function(desc) {
        console.log(desc);
        $http.post('/view1', desc).
        then(function(response) {
            console.log("posted successfully");
        }).catch(function(response) {
            console.error("error in posting");
        })
    }
});

这是我的 server.js:

And this is my server.js:

app.post('/view1', function(req, res) {
    console.log(res.desc);
    res.end();
});

我没有使用过body-parser.当我在控制器中使用函数时,我不明白如何使用 body-parser 从 html 获取表单值.使用 body-parser 时单击提交时是否从 html 中获取值,或者是否从我将表单值作为参数传递的函数中获取值.请告诉我它是如何完成的.

I have not used body-parser. I don't get how body-parser is used to get the form values from the html when I am using a function in controller. Does the values got from the html on clicking submit when using body-parser or does the values are got from the function on which I am passing the form values as arguments. Please tell me how it is done .

这是我的 html:

<form>
      <input type="text" ng-model="desc" placeholder="Enter desc" />
      <button class="btn btn-primary" ng-click="sub(desc)">Submit</button>
</form>

编辑 2:完整的 server.js 代码:

EDIT 2: full server.js code:

var express = require('express'),
    http = require('http'),
    path = require('path'),
    bodyParser= require('body-parser');

var app = express();

app.set('port', 3000);

app.use(express.static(path.normalize(__dirname + '/')));
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing       application/x-www-form-urlencoded

app.get('/main', function(req, res) {
    var name = 'MyNameFromServer';
    res.send(name);
});

app.post('/view1', function(req, res) {
    console.log(res.body.desc);
    res.end();
});

http.createServer(app).listen(app.get('port'), function() {
    console.log('Express server listening on port ' + app.get('port'));
});

编辑 3:编辑控制器 app.js

EDIT 3: Edited Controller app.js

app.controller('view1Ctrl', function($scope, $http) {    
    $scope.sub = function() {
        console.log($scope.formData);
        $http.post('/view1', $scope.formData).
        success(function(data) {
            console.log("posted successfully");
        }).error(function(data) {
            console.error("error in posting");
        })
    };
});

推荐答案

Node.js (Express) 的 body-parser 模块可以将表单帖子中的每个数据放入一个名为 req.body,所以如果你有一个 $scope 对象来定义你的表单数据,你可以直接注入它以在 req.body 上复制相同的属性:

The body-parser module for Node.js (Express) can get every data from your form post into a single object called req.body, so if you have a $scope object to define your form data you can inject directly that to have the same properties copied on req.body:

角度:

app.controller('view1Ctrl', function($scope, $http) {
    $scope.sub = function() {
        $http.post('/view1',$scope.formData).
        then(function(response) {
            console.log("posted successfully");
        }).catch(function(response) {
            console.error("error in posting");
        })
    }
});

HTML:

<form>
      <input type="text" ng-model="formData.desc" placeholder="Enter desc" />
      <input type="text" ng-model="formData.title" placeholder="Enter title" />
      <button type="submit" class="btn btn-primary" ng-click="sub()">Submit</button>
</form>

现在当您通过 $http.post('/view1', $scope.formData) 提交时,您将获得相同的对象,例如:

Now when you submit it via $http.post('/view1', $scope.formData)you will get the same object, for example:

app.post('/view1', function(req, res) {
    console.log(req.body.desc);
    res.end();
});

您也可以在表单元素中使用 ng-submit 代替在提交按钮上单击 ng-click,如下所示:

Instead having an ng-click on the submit button, you could also use ng-submitin the form element like this:

<form ng-submit="sub()">

这篇关于ExpressJS AngularJS POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆