In this article, we will see how the input Directive in AngularJS can be utilized to change the default behavior of elements. This can be done with the help of the ng-model attribute present inside the element. 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:
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>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
script>
<title>AngularJS input Directivetitle>
head>
<body ng-app="" style="text-align:center;">
<h1 style="color:green">
GeeksforGeeks
h1>
<h3>AngularJS input Directiveh3>
<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>
<head>
<title> AngularJS input Directivetitle>
<style>
input.ng-invalid {
background-color: green;
}
input.ng-valid {
background-color: yellow;
}
style>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
script>
head>
<body ng-app="" style="text-align:center;">
<h1 style="color:green">
GeeksforGeeks
h1>
<h3>AngularJS input Directiveh3>
<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:
