什么是使用ANGULARJS表单验证,而不是jQuery的形式验证插件的好处? [英] What are the benefits of using ANGULARJS form validation instead of jquery form validation plugin?

查看:99
本文介绍了什么是使用ANGULARJS表单验证,而不是jQuery的形式验证插件的好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个很奇怪的问题。我开始使用AngularJS,我必须用AngularJS的表单验证的问题。

This might be a very weird question. I am starting to use AngularJS and I have a question on form validation using AngularJS.

我的问题是,为什么我会用使用AngularJS,如果我可以做同样的使用jQuery表单验证表单验证。对于过去4年的差不多了,一直没有形成我无法验证轻易使用jquery表单验证插件。我想知道采用了棱角分明的JS表单验证,而不是真正的考验jQuery的表单验证

My question is why would I use form validation using AngularJS if I can do the same using jQuery form validation. For last 4 years almost, there has been no forms I could not validate easily using jquery form validation plugin. I WANT to know the benefits of using Angular JS form validation instead of truly tested jquery form validation

推荐答案

我想主要的好处是能够测试和易用性。而更复杂的jQuery验证是通过JavaScript做角方法声明表单验证。角验证落在同程HTML5元素应验证线,它认为所有验证约束,应在标记,而不是在code(我同意)声明。

I think the main benefit is the ability to testing and ease of use. Angular approaches form validation declaratively whereas more complex JQuery validation is done via JavaScript. Angular validation falls in line with the way HTML5 elements should be validated and it believes all validation constraints should be declared in the markup rather than in code (which I agree).

最大的问题(在我看来),在角度验证是,虽然这是很好的初步实践,以decalare显示的数据的元素在你的验证规则,我不相信这是很好的做法,也必须定义您的验证在标记的消息在同一时间。

The biggest problem (in my opinion) with validation in angular is that while it is good pratice to decalare your validation rules on the element that displays the data I don't believe it is good practice to have to also define your validation messages in markup at the same time.

<form name="signupFrm" novalidate="novalidate">  
  <div classs="form-row">
    <label>Username:</label>
    <input name="username" 
    ng-model="model.username"
    ng-minlength="3"
    ng-maxlength="10"
    required/>
    <div class="errors" 
    ng-show="signupFrm.username.$dirty && signupFrm.username.$invalid">
      <small class="error" ng-show="signupFrm.username.$error.required">Please input a username</small>
      <small class="error" ng-show="signupFrm.username.$error.minlength">Please enter a min length of 3</small>
      <small class="error" ng-show="signupFrm.username.$error.maxlength">Please enter a max length of 10</small>
  </div>
</div>

在大型应用这个方法很快就会难以管理以及强制逻辑放到标记这是从来没有好,因为它可以不容易被测试。

On large application this quickly becomes unmanageable as well as forcing logic into markup which is never good as it cannot as easily be tested.

不过,我还是青睐这种过度jQuery和我创建了一个角模块角汽车-validate 其目的是在标记定义邮件摆脱的需要。这需要认为,尽管角表单验证的声明自然是伟大的,你居然显示错误信息给用户(与前pressions)的方式导致更多不可维护的; code,因此该模块通过移动应用动态验证信息,以减少在你的标记的复杂性。

However, I would still favour this over JQuery and I have created a angular module angular-auto-validate which seeks to get rid of the need to defined your messages in the markup. It takes the view that while the declarative nature of the angular form validation is great the way you actually show error messages to the user (with expressions) leads to more unmaintainable; code hence this module moves to reduce that complexity in your markup by applying dynamic validation message.

您可以找到该模块这里和观念背后的角博客文章< A HREF =htt​​p://jonsamwell.com/dynamic-angularjs-validation/相对=nofollow>这里

You can find the module here and the angular blog post behind the idea here

这篇关于什么是使用ANGULARJS表单验证,而不是jQuery的形式验证插件的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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