Skip to content
Related Articles

Related Articles

Improve Article

AngularJS | input Directive

  • Last Updated : 25 Jul, 2019
Geek Week

Input is an HTML tag which is used to get user input.ng-model is an angular directive which is used for data binding(i.e.,reference to the input element is provided by the ng-model). These both are combined to modify the input field.

Syntax:

<input ng-model="name">

The following states are established to the input field whose value is true for the following.

  • $untouched: when the field has not been touched
  • $touched: when the field has been touched
  • $pristine: when the field has not been modified
  • $dirty: when the field has been modified
  • $invalid: when the field content is not valid
  • $valid: when the field content is valid

Example: $valid state for the required input field in a form is explained below




<!DOCTYPE html>
  
<html>
  
<script src=
</script>
  
<body ng-app="">
    <center>
        <h1 style="color:green">
        GeksforGeeks
    </h1>
  
        <form name="form1">
  
            <input name="var1"
                ng-model="var1"
                required>
        </form>
  
        <p>{{var1}}</p>
  
        <p>The state of $valid is {{form1.var1.$valid}}</p>
    </center>
</body>
  
</html>                    

Output:
Before:



After:

For the above-mentioned input directive, few CSS classes are added. They are:

  • ng-untouched: The field has not been touched yet
  • ng-touched: The field has been touched
  • ng-pristine: The field has not been modified yet
  • ng-dirty: The field has been modified
  • ng-valid: The field content is valid
  • ng-invalid: The field content is not valid
  • ng-valid-key: One key for each validation.
    Example: ng-valid-required
  • ng-invalid-key: One key for each validation.
    Example: ng-invalid-required

The classes are removed if the value is false.

Example:
If the input field is used along with the required attribute, valid (when there is an input it is set to true) and invalid (when there is no input it is set to true) states are established. The classes are removed if the value is false.




<!DOCTYPE html>
  
<html>
  
<head>
    <title>
        input Directive
    </title>
    <style>
        input.ng-invalid {
            background-color: green;
        }
          
        input.ng-valid {
            background-color: yellow;
        }
    </style>
</head>
<script src=
</script>
  
<body ng-app="">
    <center>
        <h1 style="color:green">
        GeksforGeeks
    </h1>
  
        <form name="form1">
  
            <input name="var1"
                   ng-model="var1"
                   required>
        </form>
  
        <p>{{var1}}</p>
  
        <p>
          The state of $valid is
          {{form1.var1.$valid}}
      </p>
    </center>
</body>
  
</html>

Output:
Before:

After:




My Personal Notes arrow_drop_up
Recommended Articles
Page :