简体   繁体   English

角度表单验证:当至少一个输入为ng-invalid和ng-dirty时显示ng-ng

[英]Angular form validation: ng-show when at least one input is ng-invalid and ng-dirty

I have the following form in an Angular partial: 我在Angular partial中有以下表格:

<form name="submit_entry_form" id="submit_entry_form" ng-submit="submit()" ng-controller="SubmitEntryFormCtrl" novalidate >
    <input type="text" name="first_name" ng-model="first_name" placeholder="First Name" required/><br />
    <input type="text" name="last_name" ng-model="last_name" placeholder="Last Name" required/><br />
    <input type="text" name="email" ng-model="email" placeholder="Email Address" required/><br />
    <input type="text" name="confirm_email" ng-model="confirm_email" placeholder="Confirm Email Address" required/><br />
    <span ng-show="submit_entry_form.$invalid">Error!</span>
    <input type="submit" id="submit" value="Submit" />
</form>

The trouble I'm having is with the span at the bottom that says "Error!". 我遇到的麻烦是底部的跨度是“错误!”。 I want this to show ONLY if one of the inputs is both "ng-dirty" and "ng-invalid". 我希望这只显示其中一个输入是“ng-dirty”和“ng-invalid”。 As it is above, the error will show until the form is completely valid. 如上所述,错误将显示,直到表单完全有效。 The long solution would be to do something like: 长期的解决方案是做类似的事情:

<span ng-show="submit_entry_form.first_name.$dirty && submit_entry_form.first_name.$invalid || submit_entry_form.last_name.$dirty && submit_entry_form.last_name.$invalid || submit_entry_form.email.$dirty && submit_entry_form.email.$invalid || submit_entry_form.confirm_email.$dirty && submit_entry_form.confirm_email.$invalid">Error!</span>

Which is UGLY. 哪个是UGLY。 Any better way to do this? 有更好的方法吗?

Method 1: Use a function on $scope set up by your controller. 方法1:在控制器设置的$ scope上使用函数。

So with a better understanding of your problem, you wanted to show a message if any field on your form was both $invalid and $dirty ... 因此,为了更好地理解您的问题,您希望在表单上的任何字段同时为$ invalid和$ dirty时显示一条消息...

Add a controller method: 添加控制器方法:

app.controller('MainCtrl', function($scope) {

  $scope.anyDirtyAndInvalid = function (form){
    for(var prop in form) {
      if(form.hasOwnProperty(prop)) {
         if(form[prop].$dirty && form[prop].$invalid) {
           return true;
         }
      }
    }
    return false;
  };
});

and in your HTML: 并在您的HTML中:

<span ng-show="anyDirtyAndInvalid(submit_entry_form);">Error!</span>

Here is a plunker to demo it 这是一个演示它的plunker

Now all of that said, if someone has entered data in your form, and it's not complete, the form itself is invalid. 现在,所有的这么说,如果有人在你的表单中输入的数据,这是不完全的,形式本身无效的。 So I'm not sure this is the best usability. 所以我不确定这是最好的可用性。 But it should work. 但它应该工作。


Method 2: Use a Filter! 方法2:使用过滤器! (recommended) (推荐的)

I now recommend a filter for this sort of thing... 我现在推荐一种过滤器用于此类事情......

The following filter does the same as the above, but it's better practice for Angular, IMO. 以下过滤器与上面的过滤器相同,但对于Angular,IMO来说,这是更好的做法。 Also a plunk. 也是一个插件。

app.filter('anyInvalidDirtyFields', function () {
  return function(form) {
    for(var prop in form) {
      if(form.hasOwnProperty(prop)) {
        if(form[prop].$invalid && form[prop].$dirty) {
          return true; 
        }
      }
    }
    return false;
  };
});
<span ng-show="submit_entry_form | anyInvalidDirtyFields">Error!</span>

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

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