[英]how to get data from angularjs controller
hey guys may i know how to get data from controller ? 大家好,我知道如何从控制器中获取数据吗?
for example 例如
controller.js controller.js
function RegisterCtrl($scope, $http, $location) {
$scope.form = {};
$scope.submitPost = function() {
$http.post('/register', $scope.form).
success(function(data) {
$location.path('/');
});
};
}
routes/index.js 路线/index.js
app.post('/register', sessionHandler.handleSignup);
routes/session.js 路线/session.js
this.handleSignup = function(req, res, next) {
"use strict";
// when i try to access into the controller by using "data" it said my "data"
// is not declare, what should i pass into `var data` = ?? in order to
// access the data from controller
var email = data.email,
confirmEmail = data.confirmEmail,
password = data.password,
confirmPassword = data.confirmPassword,
firstName = data.firstName,
lastName = data.lastName,
penName = data.penName,
publicUsername = data.publicUsername;
}
You have to use the express.urlencoded()
and express.json()
middleware in order to parse body data that was sent via HTTP
DELETE
/ POST
/ PUT
. 您必须使用
express.urlencoded()
和express.json()
中间件才能解析通过HTTP
DELETE
/ POST
/ PUT
发送的主体数据。
After that, there will be a body
property on the req
object and you can use it like that: 之后,
req
对象上将有一个body
属性,您可以像这样使用它:
this.handleSignup = function(req, res, next) {
"use strict";
var data = req.body,
email = data.confirmEmail,
// etc.
};
from the success response in your http post you could just assign a scope to it then display it to your partials. 从http帖子中的成功响应中,您可以为其分配一个范围,然后将其显示给您的局部。 Something like this.
这样的事情。
$http.post('/register', $scope.form).
success(function(data) {
$scope.data = data;
});
Then from your partials/view 然后从您的局部视图
<div>{{data}}</div>
Regards, 问候,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.