required 和 ng-required 有什么区别? [英] What is the difference between required and ng-required?
问题描述
required
和 ng-required
(表单验证)有什么区别?
What is the difference between required
and ng-required
(form validation)?
推荐答案
AngularJS 表单元素寻找 required
属性来执行验证功能.ng-required
允许您根据布尔测试设置 required
属性(例如,只需要字段 B - 比如说,一个学生号- 如果字段 A 具有特定值 - 如果您选择了学生"作为选项)
AngularJS form elements look for the required
attribute to perform validation functions. ng-required
allows you to set the required
attribute depending on a boolean test (for instance, only require field B - say, a student number - if the field A has a certain value - if you selected "student" as a choice)
举个例子, 和
本质上是一回事
As an example, <input required>
and <input ng-required="true">
are essentially the same thing
如果您想知道为什么这是这种方式,(而不仅仅是使 或
),这是由于 HTML 的限制 -
required
属性没有关联的值 - 它的存在意味着(根据 HTML 标准)该元素是必需 - 所以 angular 需要一种设置/取消设置所需值的方法(required="false"
将是无效的 HTML)
If you are wondering why this is this way, (and not just make <input required="true">
or <input required="false">
), it is due to the limitations of HTML - the required
attribute has no associated value - its mere presence means (as per HTML standards) that the element is required - so angular needs a way to set/unset required value (required="false"
would be invalid HTML)
这篇关于required 和 ng-required 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!