1

I have one template

<form ng-submit="save()" ng-controller="formController">
  <input type="text" ng-model="textValue1" required /></br>
  <input type="text" ng-model="textValue2" ng-required="true" /></br>
  <input type="submit" value="Submit">
</form>

here both are validating fine when i clicked submit button,may i know which scenario these things can use exactly in angularjs.

2 Answers2

0

'AngularJS form elements look for the required attribute to perform validation functions. ng-required allows you to set the required attibute 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 - it's 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)'

Source: What is the difference between required and ng-required?

Community
  • 1
  • 1
uksz
  • 18,239
  • 30
  • 94
  • 161
0

ng-required="condition" is similar to required="{{condition}}". No difference than this.

Kiran Reddy
  • 556
  • 3
  • 11
  • Ok Kiran Reddy no difference,then may i know why 2 attributes here required and ng-required.can u tell exact place which is useful without any issues. – vallepu veerendra kumar Apr 09 '15 at 07:36
  • 2
    If you want to give required to an input and don't want to change it later as in your code both behave similarly. But if you want to update the required attribute dynamically based on a condition/variable then it is just a difference in the syntax, nothing more than that. – Kiran Reddy Apr 09 '15 at 07:43