所以我使用ng-submit和ng-model制作一个带有角度js的用户注册表单。问题是用户提交表单的触发器两次。
我四处寻找共同的原因,但没有一个适合这个法案。我没有宣布我的控制器两次,我的提交按钮没有任何ng-click事件。
这是表单代码(这里可能出现了我错过的错误)
<form class="form" ng-submit="registerUser()">
<div class="form-group row">
<label for="user-fname">First Name</label>
<div class="two-for-one">
<input type="text" id="user-fname" name="user-fname" class="form-control" placeholder="first name" ng-model="userData.fname">
</div>
<label for="user-lname">Last Name</label>
<div class="two-for-one">
<input type="text" id="user-lname" name="user-lname" class="form-control" placeholder="last name" ng-model="userData.lname">
</div>
</div>
<label for="email">Email</label>
<input class="form-control" type="text" id="email" name="email" placeholder="Email" rel="popover" data-content="What’s your email address?" ng-model="userData.email">
<label for="reg_password">Password</label>
<input class="form-control" type="password" id="reg_password" name="reg_password" placeholder="Password" rel="popover" data-content="Please choose a password (minimum of 6 characters)" ng-model="userData.pw">
<label for="reg_password_confirm">Confirm Password</label>
<input class="form-control" type="password" id="reg_password_confirm" name="reg_password_confirm" placeholder="Confirm Password" rel="popover" data-content="Please confirm your password" ng-model="userData.cpw">
<label for="mail">Want to be apart of our mailing list?</label>
<input type="checkbox" id="mail" name="mail" value="1" ng-model="userData.mail">
<button class="btn btn-success">Next</button>
</form>
虽然我不认为这里的js是:
$scope.userData = {
acc_type: "user",
mail:false,
};
$scope.registerUser = function() {
loginRegsterService.registerUser($scope.userData).then(function(response){
console.log(response);
});
};
最后一部分是使用http发布数据的服务。它非常标准
this.registerUser = function(data) {
var req = {
method: 'POST',
url: location.protocol + '//' + location.host + '/models/register.php',
headers: {
'Content-Type': 'application/json'
},
data: data
}
return $http(req).then(function(response) {
return response.data;
});
};
感谢阅读和帮助!
哦,我忘了提到这一切都很好,除了提交两次:\
除了angular指令外,html是有效的。
答案 0 :(得分:0)
也许是因为你的标签没有指定类型属性 - 并且它被视为提交intialy?另见:https://stackoverflow.com/a/4667996/405623?