按下按钮,浏览器不会发生任何操作,并且我的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