我已经对我的表单应用了验证(它只有两个字段)但不知道如何阻止它提交,当前流程是:按下回车键后,学生的名字和标记被添加到localstorage上并显示在从那里的屏幕,但我无法阻止空数据提交。
这些是我的js函数:
$scope.searchEnter = function() {
if (event.which == 13 && $scope.student != "") {
$scope.addStudent();
}
};
$scope.addStudent = function() {
if ($scope.marks > 65) {
var pass = true;
} else {
pass = false;
}
$scope.students.push({ 'studentName': $scope.student, 'Marks': parseInt($scope.marks), 'pass': pass });
$scope.student = '';
$scope.marks = '';
localStorage['studentsList'] = JSON.stringify($scope.students);
};
这是html部分:
<div class="row">
<div class="col-xs-12">
<form class="form-horizontal" novalidate name="studentForm" >
<div class="form-group">
<label class="col-sm-2 control-label" for="student_name">Student's Name</label>
<div class="col-sm-5">
<input ng-model="student" ng-keyup="searchEnter()" type="text" class="form-control" id="student_name" ng-required="true" name="stdname">
<div ng-show="studentForm.stdname.$touched && studentForm.stdname.$invalid">
<small style="color:red; display:block;">Enter a valid name </small>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="student_marks">Marks obtained</label>
<div class="col-sm-5">
<input ng-model="marks" ng-keyup="searchEnter()" type="number" class="form-control" id="student_marks" ng-required="true">Press ENTER to insert student's data in the table.</div>
</div>
</form>
</div>
</div>
答案 0 :(得分:1)
假设您的字段正确验证,以防止submit
您可以使用ngDisabled
指令,如下所示:
<button type="submit" ng-disabled="form.$invalid">Submit</button>
编辑:由于OP提供了完整代码,我能够给出正确答案,即:
将支票更改为:
if (event.which == 13 && $scope.student && $scope.marks) {
代码段根据您的code
:
(function() {
angular
.module('app', [])
.controller('MainCtrl', MainCtrl);
MainCtrl.$inject = ['$scope'];
function MainCtrl($scope) {
$scope.students = [];
$scope.searchEnter = function() {
if (event.which == 13 && $scope.student && $scope.marks) {
$scope.addStudent();
}
};
$scope.addStudent = function() {
console.log('addStudent called');
$scope.students.push({
'studentName': $scope.student,
'Marks': $scope.marks,
'pass': $scope.marks > 65
});
$scope.student = '';
$scope.marks = '';
localStorage['studentsList'] = JSON.stringify($scope.students);
};
}
})();
&#13;
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body ng-controller="MainCtrl">
<div class="row">
<div class="col-xs-12">
<form class="form-horizontal" novalidate name="studentForm">
<div class="form-group">
<label class="col-sm-2 control-label" for="student_name">Student's Name</label>
<div class="col-sm-5">
<input ng-model="student" ng-keyup="searchEnter()" type="text" class="form-control" id="student_name" ng-required="true" name="stdname">
<div ng-show="studentForm.stdname.$touched && studentForm.stdname.$invalid">
<small style="color:red; display:block;">Enter a valid name </small>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="student_marks">Marks obtained</label>
<div class="col-sm-5">
<input ng-model="marks" ng-keyup="searchEnter()" type="number" class="form-control" id="student_marks" ng-required="true">Press ENTER to insert student's data in the table.</div>
</div>
</form>
</div>
</div>
</body>
</html>
&#13;
<强>提示:强>
ngModel
$scope.marks
已经是一个号码,您不需要执行任何parse
,因此您可以拥有'Marks': $scope.marks
。< / p>
pass
的检查可以简化为:'pass': $scope.marks > 65