angularjs - ExpressJS AngularJS POST

标签 angularjs node.js express

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

这是我的 Controller :

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:

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

我没有使用过body-parser。当我在 Controller 中使用函数时,我不知道如何使用 body-parser 从 html 获取表单值。使用 body-parser 时单击提交时从 html 中获取的值还是从我将表单值作为参数传递的函数中获取的值。请告诉我它是如何完成的。

编辑:这是我的 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 代码:

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: 编辑 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 对象来定义您可以直接注入(inject)的表单数据,以便在 req.body 上复制相同的属性:

Angular :

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)提交时你会得到相同的对象,例如:

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

您也可以在表单元素中使用 ng-submit,而不是 ng-click 提交按钮:

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

关于angularjs - ExpressJS AngularJS POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31119605/

相关文章:

javascript - 模板上 ng-repeat 中的 AngularJS 指令链接范围

javascript - 从外部调用 AngularJS Controller 函数

mysql - Sequelize 多连接中的排序错误

javascript - 使用回调对异步函数进行单元测试

node.js - 检查用户是否只更改他的数据

javascript - 如何在同一台服务器上处理 tcp/ip 原始请求和 http 请求

javascript - Angular-ui-router 基于参数的不同模板

javascript - Express.js 请求参数未定义

javascript - MEAN API 请求方法问题

angularjs - Angular 计时器不工作