如何从angularjs控制器获取数据

时间:2013-11-23 02:15:56

标签: 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('/');
      });
  };

}

路由/ index.js

app.post('/register', sessionHandler.handleSignup);

路由/ 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;
}

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:0)

从您的http帖子中的成功响应中,您可以为其分配范围,然后将其显示给您的部分。这样的事情。

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

然后从你的部分/视图

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

此致