如何计算表单上的错误数量? [英] How can i count the number of errors on Form?

查看:27
本文介绍了如何计算表单上的错误数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FIDDLE

我如何计算表单上的错误次数?

HTML

 

抱歉,出现了 3 个错误.

解决方案

一种方法可以通过使用特定错误条件的特定计数、requiredpattern 等等,它们可以作为表单的 $error[prop] 数组的一部分使用.在您的情况下,您可以尝试使用 form.$error.required.length:-

<div ng-show="form.$submitted && form.$invalid">抱歉,出现了 {{form.$error.required.length}} 错误.

演示

您可以在控制器上添加一个函数,该函数可以确定表单上的错误数量并返回它,并在视图中使用它来显示总错误计数.

 $scope.numberoferrors=function(form){无功计数 = 0,错误 = form.$error;angular.forEach(errors, function(val){ if(angular.isArray(val)) { count += val.length; } });//Object.keys(errors).forEach(function(key){ count += errors[key].length });//从表单中获取所有错误类别的计数返回计数;};

演示

FIDDLE

How can i count the number of errors made on form ?

HTML

 <div ng-show="form.$submitted && form.$invalid">
      Sorry but 3 errors have been made.
 </div>

解决方案

One way you could do this by using the specific count of a specific error criteria, required, pattern etc.. that are available as a part of form's $error[prop] array. In your case you could try using form.$error.required.length:-

<div ng-show="form.$submitted && form.$invalid">
       Sorry but {{form.$error.required.length}} errors have been made.
</div>

Demo

You could add a function on the controller which can determine the number of errors on the form and return it, and use it in the view to display the total error count.

 $scope.numberoferrors=function(form){
    var count = 0,
        errors = form.$error;

     angular.forEach(errors, function(val){ if(angular.isArray(val)) {  count += val.length; }  });
    //Object.keys(errors).forEach(function(key){ count += errors[key].length  }); //get count of all error categories from form

    return count;
 };

Demo

这篇关于如何计算表单上的错误数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆