按下按鈕,瀏覽器不會發生任何操作,並且我的REST端點上沒有新用戶。 registerUser()由按鈕按下觸發。我密切關注教程。AngularJs Http發佈不發帖
的HTML部分
<div class="section no-pad-bot" ng-controller="registerController" id="index-banner">
等等等等然後
<form>
<div class="row">
<div class=" input-field col s6 offset-s3">
<i class="material-icons prefix">account_circle</i>
<input type="text" class="validate" ng-model="user.username" placeholder="Username">
</div>
</div>
<div class="row">
<div class=" input-field col s6 offset-s3">
<i class="material-icons prefix">account_circle</i>
<input type="text" class="validate" ng-model="user.email" placeholder="Email">
</div>
</div>
<div class="row">
<div class=" input-field col s6 offset-s3">
<i class="material-icons prefix">account_lock</i>
<input type="text" class="validate" ng-model="user.password" placeholder="Password">
</div>
</div>
<button class="btn waves-effect waves-light" type="submit" ng-click="registerUser()">Submit
<i class="material-icons right">send</i>
</button>
</form>
在clientapp.js
myApp.controller('registerController', function ($scope, $http) {
$scope.registerUser = function() {
// use $.param jQuery function to serialize data from JSON
var data = $.param({
hashword: $scope.password,
username: $scope.username,
email: $scope.email,
});
var config = {
headers : {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
}
$http.post('/api/users', data, config)
.success(function (data, status, headers, config) {
console.log("successful post");
$scope.PostDataResponse = data;
})
.error(function (data, status, header, config) {
console.log("failed post");
$scope.ResponseDetails = "Data: " + data +
"<hr />status: " + status +
"<hr />headers: " + header +
"<hr />config: " + config;
});
};
我不明白'你的範圍user'對象,改變'NG-模型= 「user.email」'到NG-模型=「電子郵件」類似地'ng-model =「user.username」'將'ng-model =「username」'和'ng-model =「user.password」'轉換爲'ng-model =「password」' – manish