What is the difference between required and ng-required?

TidharPeer picture TidharPeer · May 20, 2013 · Viewed 127.7k times · Source

What is the difference between required and ng-required (form validation)?

Answer

Tiago Roldão picture Tiago Roldão · May 20, 2013

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

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)