Angularjs表单验证错误在表单中给出novalidate时未显示

Angularjs表单验证错误在表单中给出novalidate时未显示,angularjs,Angularjs,提交表单标记时,我希望显示自定义错误消息 我的表格是 <form class="form-horizontal" role="form" id="new-session-form" name="saveSessionForm" ng-model="saveSessionForm" ng-submit="saveSession()" novalidate> <textarea class="form-control" name="sessionTopic" ng-model="

提交表单标记时,我希望显示自定义错误消息

我的表格是

<form class="form-horizontal" role="form" id="new-session-form" name="saveSessionForm" ng-model="saveSessionForm" ng-submit="saveSession()" novalidate>

<textarea class="form-control" name="sessionTopic" ng-model="session.topic" required></textarea>

<div ng-show="submitted && saveSessionForm.sessionTopic.$error.required" class="error">Session Name cannot be blank.</div>

<button type="submit" class="btn btn-sm ng-click="submitted=true">Save</button>

会话名称不能为空。

根据您更新的要求进行编辑:

对HTML中的元素进行单独的验证:

<textarea class="form-control" name="sessionTopic" ng-model="session.topic" required></textarea>
<div ng-show="saveSessionForm.sessionTopic.$error.required" class="error">Session topic cannot be blank.</div>

实现这一目标的两种方法:

首先,如果要在提交时进行验证:

<div ng-show="errorExists" class="error">Session Name cannot be blank.</div>

$scope.saveSession = function(){
    if ($scope.saveSessionForm.$valid) {
       $scope.errorExists = false;
        alert("Saving");
    } else {
       $scope.errorExists = true;
    }                
}
会话名称不能为空。
$scope.saveSession=function(){
if($scope.saveSessionForm.$valid){
$scope.errorExists=false;
警报(“保存”);
}否则{
$scope.errorExists=true;
}                
}

第二,在键入和删除时:

<div ng-show="saveSessionForm.$valid" class="error">Session Name cannot be blank.</div>
会话名称不能为空。

/////////////////////////////////////////////////////////////////////////////////////////////

novalidate
基本上意味着表格在提交时不进行验证:

因此,从技术上讲,您是在告诉它[使用
必需的
]进行验证,然后使用
诺瓦利达
进行否定。这就是为什么除非您删除
novalidate
,否则它将不起作用


换句话说,
novalidate
覆盖了
required
。如果您想制定自己的JavaScript验证规则,您可能需要使用它。

您所说的适用于“普通”HTML5。这是一个AngularJS应用程序,事情有点不同:)谢谢你的回复。。。但是当我使用submitted&&saveSessionForm.sessionTopic.$error.required时,为什么它不显示消息,因为它应该可以正常工作。。。像这样,我还有3个其他字段,那么对于每个字段,我在valid中写false,在else块中写true……你能准备一把小提琴来演示这个问题吗?
<div ng-show="saveSessionForm.$valid" class="error">Session Name cannot be blank.</div>