AngularJS ng-required 指令

定义和用法

ng-required 指令设置表单字段(输入或文本区域)的 required 属性。

如果 ng-required 属性中的表达式返回 true,则表单字段将是必填项。

ng-required 指令对于在 true 和 false 之间切换值是必要的。在 HTML 中,您不能将 required 属性设置为 false(required 属性的存在会使元素成为必填项,无论其值如何)。

Example

Set the input field to required:

Required:

<input type="checkbox" ng-model="myVar">
<input name="myInput" ng-model="myInput" ng-required="myVar">

Try it yourself

Syntax

<input ng-required="expression</input>

Affected <input>,<select> and <textarea> and other editable elements supported.

Parameters

Parameters Description
expression If it returns true, the expression that sets the required attribute will be set.