AngularJS input Directive


An input field with information binding:

<input ng-model="myInput">

<p>The estimation of the information field is:</p>
Try it yourself »

Definition and Usage

AngularJS changes the default conduct of <input> components, in any case, just if the ng-model characteristic is present.

They give information official, which implies they are a piece of the AngularJS display, what's more, can be alluded to, and refreshed, both in AngularJS capacities and in the DOM.

They give approval. Precedent: a <input> component with a required characteristic, has the $valid state set to false as long as it is empty.

They likewise give state control. AngularJS holds the present condition of all input elements.

Input fields have the accompanying states:

  • $untouched The field has not been contacted yet
  • $touched The field has been touched
  • $pristine The field has not been changed yet
  • $dirty The field has been modified
  • $invalid The field content isn't valid
  • $valid The field content is valid

The estimation of each state speaks to a Boolean esteem, and is either true or on the other hand false.


<input ng-model="name">

Input components are being alluded to by utilizing the estimation of the ng-model attribute.

CSS Classes

<input> components inside an AngularJS application are given sure classes. These classes can be utilized to style input components as indicated by their state.

The following classes are added:

  • ng-untouched The field has not been contacted yet
  • ng-touched The field has been touched
  • ng-pristine The field has not been  adjusted yet
  • ng-dirty The field has been modified
  • ng-valid The field content is valid
  • ng-invalid The field content isn't valid
  • ng-substantial key One key for every approval. Model: ng-substantial required, valuable when there are mutiple thing that must be validated
  • ng-invalid-key Example: ng-invalid-required

The classes are expelled if the esteem they speak to is false.


Apply styles for legitimate and invalid information components, utilizing standard CSS:

input.ng-invalid {
    foundation shading: pink;
input.ng-substantial {
    foundation shading: lightgreen;
Try it Yourself »