简体   繁体   English

为什么 myForm.$submitted 不能用于 ng-messages 而是用于 ng-if?

[英]Why does myForm.$submitted not work with ng-messages but with ng-if?

I am trying to validate my Form when there is a keypress and also when it its submitted.For that purpose i am writing this code :-我正在尝试在有按键和提交时验证我的表单。为此,我正在编写此代码:-

<form name="myForm" ng-submit="submit()" novalidate>
    <input type="email" name="email" ng-model="email" required/>
    <div ng-messages="myForm.$submitted">
        <span ng-message="required">Please enter details in these field</span>
        <span ng-message="email">Please enter email</span>
    </div>
    <button type="submit">Save</button>
</form>

There is a success message in submit function :-提交功能中有一条成功消息:-

$scope.submit = function(){
   console.log("Update Successful");
}

Even if i haven't fill the required field and press Save i still get the "Update Successful" message.So,why doesn't the validation work and why is the submit function even if the validation fails.即使我没有填写必填字段并按保存,我仍然收到“更新成功”消息。那么,为什么验证不起作用,为什么即使验证失败,提交功能也是如此。

Also i found these solution of doing it these way :-我也找到了这些解决方法:-

<form name="myForm" ng-submit="myForm.$valid && submit()"  novalidate>
    <input type="email" name="email" ng-model="email" required/>
    <div ng-messages="myForm.email.$error" ng-if="myForm.$submitted">
        <span ng-message="required">Please enter details in these field</span>
        <span ng-message="email">Please enter email</span>
    </div>
    <button type="submit">Save</button>
</form>

This works fine but problem is,it should also validated on keypress.However,it only validates on keypress after i have sumbitted the form atleast once before that keypress validation doesn't work.这工作正常,但问题是,它也应该在按键上验证。但是,它仅在我至少在按键验证不起作用之前对表单进行一次 sumbitted 之后才在按键上验证。 How should i solve these?我应该如何解决这些问题? I was also trying myForm.$touched but even that doesn't work when i use it as :-我也在尝试 myForm.$touched 但是当我将它用作:-

<div ng-messages="myForm.$touched">
    ...
</div>

Try this:尝试这个:

In html:在 HTML 中:

<form name="myForm" novalidate>
    <input type="email" name="email" required/>
    <div ng-messages="myForm.email.$error" ng-if="myForm.email.$touched || valid">
        ...
    </div>
    <button ng-click="submit(myForm.$valid)">Save</button>
</form>

In controller:在控制器中:

$scope.submit(valid)
{
    valid ? $scope.validCheck = false : $scope.validCheck = true;
}

There is a little something that you've missed in implementing AngularJS 's form validation.在实现AngularJS的表单验证时,您遗漏了一些东西。

From the code you've provided, your form, as it seems, is using the default HTML5 form validation and NOT AngularJS form validation.从您提供的代码来看,您的表单似乎使用了默认的 HTML5 表单验证,而不是AngularJS 表单验证。

How?如何?

In order to be able to wire up with AngularJS form validation (technically adding it as a property to the form directive), in addition to the name attribute of the form control, ng-model attribute is also required .为了能够连接 AngularJS 表单验证(技术上将其作为属性添加到form指令中),除了表单控件的name属性外,还需要ng-model属性。

Meanwhile, to disable HTML5 default validation behavior, novalidate attribute must be added to the form tag.同时,要禁用 HTML5 默认验证行为,必须将novalidate属性添加到表单标记中。

To be able to achieve your expected behavior from the form ( ie validation on key press as well as on submission , if I'm right) you can implement a combination of yourForm.$dirty and yourForm.$submitted properties:为了能够从表单中实现您预期的行为(即按键验证以及提交验证,如果我是对的),您可以实现yourForm.$dirtyyourForm.$submitted属性的组合:

<div ng-messages="myForm.email.$error" ng-if="myForm.$dirty || myForm.$submitted">
    <p ng-message="required">Please enter details in these field</p>
    <p ng-message="email">Please enter email</p>
</div>

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM