简体   繁体   中英

how to get data from angularjs controller

hey guys may i know how to get data from controller ?

for example

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

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

routes/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 .

After that, there will be a body property on the req object and you can use it like that:

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. Something like this.

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

Then from your partials/view

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

Regards,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM