AngularJS | input Directive

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:
Before:

After:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.