Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从angularjs控制器获取数据_Javascript_Angularjs_Express - Fatal编程技术网

Javascript 如何从angularjs控制器获取数据

Javascript 如何从angularjs控制器获取数据,javascript,angularjs,express,Javascript,Angularjs,Express,嘿,伙计们,我可以知道如何从控制器获取数据吗 比如说 controller.js function RegisterCtrl($scope, $http, $location) { $scope.form = {}; $scope.submitPost = function() { $http.post('/register', $scope.form). success(function(data) { $location.path('/');

嘿,伙计们,我可以知道如何从控制器获取数据吗

比如说

controller.js

function RegisterCtrl($scope, $http, $location) {
    $scope.form = {};
  $scope.submitPost = function() {
    $http.post('/register', $scope.form).
      success(function(data) {
        $location.path('/');
      });
  };

}
app.post('/register', sessionHandler.handleSignup);
 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;
}
routes/index.js

function RegisterCtrl($scope, $http, $location) {
    $scope.form = {};
  $scope.submitPost = function() {
    $http.post('/register', $scope.form).
      success(function(data) {
        $location.path('/');
      });
  };

}
app.post('/register', sessionHandler.handleSignup);
 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;
}
路由/session.js

function RegisterCtrl($scope, $http, $location) {
    $scope.form = {};
  $scope.submitPost = function() {
    $http.post('/register', $scope.form).
      success(function(data) {
        $location.path('/');
      });
  };

}
app.post('/register', sessionHandler.handleSignup);
 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;
}

从http post中的成功响应中,您可以为其分配一个作用域,然后将其显示给您的分区。像这样的

$http.post('/register', $scope.form).
  success(function(data) {
    $scope.data = data;
  });
然后从你的观点来看

<div>{{data}}</div>
{{data}

关于,

您必须使用
express.urlencoded()
express.json()
来解析通过
HTTP
/
POST
/
PUT
发送的身体数据

之后,
req
对象上将有一个
body
属性,您可以这样使用它:

this.handleSignup = function(req, res, next) {
    "use strict";

    var data = req.body,
        email = data.confirmEmail,
        // etc.
};

您好,谢谢您的回答,我是否必须在我的应用程序中包含这3个中间件?app.use(express.bodyParser());app.use(express.urlencoded());app.use(express.json())您好,谢谢您的回复,我需要将数据传递到函数中,以便在插入数据库后执行验证,而不是显示它。