Skip to content
Related Articles

Related Articles

AngularJS input Directive

Improve Article
Save Article
  • Last Updated : 04 Aug, 2022
Improve Article
Save Article

In this article, we will see how the input Directive in AngularJS can be utilized to change the default behavior of <input> elements. This can be done with the help of the ng-model attribute present inside the <input> element. <input> is an HTML tag that is used to get user data using the input.ng-model, which is an angular directive that 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

The value for each state represents the boolean value, i.e. either true or false.

Example 1: This example illustrates the $valid state for the required input field in a form.

HTML




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

Output:

 

For the above-mentioned input directive, there are few CSS classes are added, which are described below:

  • 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 2: If the input field is used along with the required attribute, then the 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. 

HTML




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

Output:

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!