简体   繁体   English

html输入模式和角度ngPattern之间的区别?

[英]Difference between html input pattern and angular ngPattern?

I notice some developers are using html input pattern for validation in an angular styled form: 我注意到一些开发人员正在使用html输入模式进行角度样式的验证:

<input id="email" name="email"
    type="text"
    ng-model="vm.contact.mail"
    pattern="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
    required />

<span ng-show="!vm.contact.email.$error.required && 
    vm.contact.email.$error.pattern && 
    vm.contact.email.$dirty">
    Email is not in the correct format.</span>

It appears to work the same with 它似乎与之相同

ng-pattern="/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/"

Are those two ways indeed interchangeable? 这两种方式确实可以互换吗? Is there any advantage/preference for one over the other in the usage or performance? 在使用或性能方面,是否有任何优势/偏好?

Form validation 表格验证

Both options will trigger form validation, since ng-pattern is just converted to pattern (tested). 两个选项都将触发表单验证,因为ng-pattern只是转换为模式(已测试)。

Difference 区别

There is just a slight difference between ng-pattern and pattern . ng-patternpattern之间只有细微的差别。 According to the documentation ng-pattern also allows you to pass Regexp expressions defined in your scope. 根据文档,ng-pattern还允许您传递范围中定义的Regexp表达式。 Also, ng-pattern will not trigger default browser validation (red border, error message around input). 此外,ng-pattern不会触发默认浏览器验证(红色边框,输入周围的错误消息)。

So you can decide whenever you want to have default browser validation in your application or not. 因此,您可以决定何时在应用程序中进行默认浏览器验证。

Example plunker showing both in action. 示例plunker显示两者都在行动。

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

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