What is the difference between required and ng-required?

前端 未结 3 1535
天涯浪人
天涯浪人 2020-11-28 19:26

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

3条回答
  •  無奈伤痛
    2020-11-28 19:49

    The HTML attribute required="required" is a statement telling the browser that this field is required in order for the form to be valid. (required="required" is the XHTML form, just using required is equivalent)

    The Angular attribute ng-required="yourCondition" means 'isRequired(yourCondition)' and sets the HTML attribute dynamically for you depending on your condition.

    Also note that the HTML version is confusing, it is not possible to write something conditional like required="true" or required="false", only the presence of the attribute matters (present means true) ! This is where Angular helps you out with ng-required.

提交回复
热议问题