v1s*_*hnu 9 node.js express angularjs
我正在学习AngularJS,我想知道如何使用ExpressJS正确地将它与Node连接.
这是我的控制器:
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");
})
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的server.js:
app.post('/view1', function(req, res) {
console.log(res.desc);
res.end();
});
Run Code Online (Sandbox Code Playgroud)
我没有使用body-parser.当我在控制器中使用函数时,我不知道如何使用body-parser从html获取表单值.使用正文解析器时,在点击提交时是否从html获取值,或者是从我将表单值作为参数传递的函数中获取值.请告诉我它是如何完成的.
编辑:这是我的HTML:
<form>
<input type="text" ng-model="desc" placeholder="Enter desc" />
<button class="btn btn-primary" ng-click="sub(desc)">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
编辑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'));
});
Run Code Online (Sandbox Code Playgroud)
编辑3: 编辑控制器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");
})
};
});
Run Code Online (Sandbox Code Playgroud)
mic*_*lem 27
Node.js(Express)的body-parser模块可以将表单中的每个数据都放到一个被调用的对象中req.body,所以如果你有一个$scope对象来定义你的表单数据,你可以直接注入,以便在req上复制相同的属性.身体:
角度:
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");
})
}
});
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
现在,当您通过提交它时,$http.post('/view1', $scope.formData)您将获得相同的对象,例如:
app.post('/view1', function(req, res) {
console.log(req.body.desc);
res.end();
});
Run Code Online (Sandbox Code Playgroud)
而不是在提交按钮上单击ng,您也可以ng-submit在表单元素中使用如下:
<form ng-submit="sub()">
Run Code Online (Sandbox Code Playgroud)
首先,你应该知道两个全局变量req和res.
当您点击发布请求时req.body捕获请求http并body-parser从发布请求中提取原始内容.
app.post('/view1', function(req, res) {
console.log(req.body.desc);
res.end();
});
Run Code Online (Sandbox Code Playgroud)
在使用它之前,你必须包括
var bodyParser = require('body-parser');
Run Code Online (Sandbox Code Playgroud)
并包含中间件
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
Run Code Online (Sandbox Code Playgroud)
详细了解middleware,req并res请参阅
| 归档时间: |
|
| 查看次数: |
25397 次 |
| 最近记录: |