2013-11-23 46 views
0

嘿傢伙我可以知道如何從控制器獲取數據嗎?如何從angularjs控制器獲取數據

例如

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; 
} 

回答

1

您必須使用express.urlencoded(),爲了解析身體數據express.json()middleware這是通過HTTP發送DELETE/POST/PUT

之後,會有req對象上的body屬性,你可以使用它像:

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

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

嗨,感謝您的回答,我是否必須在應用中包含這3箇中間件? app.use(express.bodyParser()); app.use(express.urlencoded()); app.use(express.json()) –

+0

不,只是我已經提到的兩個。 – sbruchmann

0

從你的http post中的成功響應,你可以指定一個範圍,然後將其顯示給你的部分。像這樣的東西。

$http.post('/register', $scope.form). 
    success(function(data) { 
    $scope.data = data; 
    }); 
從您的諧音

然後/視圖

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

問候,

+0

您好感謝您的回覆,我需要將數據傳遞到一個函數後,要進行驗證插入數據庫而不是顯示它。 –

相關問題