【发布时间】:2018-11-17 16:59:33
【问题描述】:
我正在尝试使用 Angular JS 对表单进行验证。该代码部分工作。除非我添加填写所有文本框,否则提交按钮将保持禁用状态。当我不填写文本框时,也会显示错误消息。
我的问题是,当我填满所有文本框并单击提交时,用户被添加,但所有文本框都清除并显示错误消息。如何在单击提交按钮时强制关闭表单?提前致谢。
if ($scope.addForm.$valid) {
alert('all inputs are valid ');
}
else {
alert('all inputs are not valid ');
}
$scope.saveUser = function () {
console.log("Saving...");
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
localStorage.setItem("users", JSON.stringify($scope.users));
};
<div class="modal-body">
<form name="addForm"class="form-horizontal" action="/action_page.php" novalidate>
<div class="form-group">
<label class="control-label col-sm-2">Email</label>
<div class="col-sm-10" ng-class="{ 'has-error' : addForm.addEmail.$invalid && !addForm.addEmail.$pristine }">
<input type="email" class="form-control" name="addEmail" placeholder="Enter Email" ng-model="newUser.email" ng-required="true">
<span class="help-block" ng-show="addForm.addEmail.$invalid && !addForm.addEmail.$pristine">
Your Email is required.
</span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Password</label>
<div class="col-sm-10" ng-class="{ 'has-error' : addForm.addPassword.$invalid && !addForm.addPassword.$pristine }">
<input type="password" class="form-control" name="addPassword" placeholder="Enter New Password" ng-model="newUser.password" ng-required="true">
<span class="help-block" ng-show="addForm.addPassword.$invalid && !addForm.addPassword.$pristine">
Your Password is required.
</span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">First Name</label>
<div class="col-sm-10" ng-class="{ 'has-error' : addForm.addFirstName.$invalid && !addForm.addFirstName.$pristine }">
<input type="text" class="form-control" name="addFirstName" placeholder="Enter First Name" ng-model="newUser.firstName" ng-required="true">
<span class="help-block" ng-show="addForm.addFirstName.$invalid && !addForm.addFirstName.$pristine">
Your First Name is required.
</span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Last Name</label>
<div class="col-sm-10" ng-class="{ 'has-error' : addForm.addLastName.$invalid && !addForm.addLastName.$pristine }">
<input type="text" class="form-control" name="addLastName" placeholder="Enter Last Name" ng-model="newUser.lastName" ng-required="true">
<span class="help-block" ng-show="addForm.addLastName.$invalid && !addForm.addLastName.$pristine">
Your Last Name is required.
</span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Contact</label>
<div class="col-sm-10" ng-class="{ 'has-error' : addForm.addContact.$invalid && !addForm.addContact.$pristine }">
<input type="tel" class="form-control" name="addContact" placeholder="Enter Contact" ng-model="newUser.contact" ng-required="true">
<span class="help-block" ng-show="addForm.addContact.$invalid && !addForm.addContact.$pristine">
Your Contact is required.
</span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Role</label>
<div class="col-sm-10" ng-class="{ 'has-error' : addForm.addRole.$invalid && !addForm.addRole.$pristine }">
<input type="text" class="form-control" name="addRole" placeholder="Enter Role" ng-model="newUser.role" ng-required="true">
<span class="help-block" ng-show="addForm.addRole.$invalid && !addForm.addRole.$pristine">
Your Role is required.
</span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Company</label>
<div class="col-sm-10" ng-class="{ 'has-error' : addForm.addCompany.$invalid && !addForm.addCompany.$pristine }">
<select class="form-control" name="addCompany" placeholder="Select Company" ng-options="company for company in companies" ng-model="newUser.company" ng-required="true">
</select>
<span class="help-block" ng-show="addForm.addCompany.$invalid && !addForm.addCompany.$pristine">
Your Company is required.
</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" ng-disabled="!addForm.$valid" ng-click="saveUser()" data-dismiss="modal">Submit</button>
</div>
</div>
</form>
</div>
更新
嗯,现在看看我的代码:一切正常plnkr.co/edit/61khc9EEKZYFvTiTFb3i?p=preview
【问题讨论】:
-
提交后是否关闭弹窗?
-
是的!即使在输入所有文本框后它也不会关闭。
-
将
bootstrap.js模态与 AngularJS 混合是自找麻烦。考虑使用 AngularJS 模态库,例如 UI-Bootstap modal