AngularJS Form Handling

AngularJS, an open-source JavaScript framework developed by Google, has been a popular choice for building interactive web applications due to its feature-rich offerings. Among these features, form handling in AngularJS is a core aspect that makes it stand out from other JavaScript frameworks. In this article, we’ll explore AngularJS form handling, its capabilities, and its benefits.

Understanding AngularJS Form Handling

Forms are a fundamental part of any web application, serving as the primary method of gathering user input. AngularJS provides a powerful form handling mechanism that extends the traditional capabilities of HTML forms. It does this through its two-way data binding feature, built-in validation methods, and the ability to create custom form validation directives.

AngularJS forms work with the concept of $scope, which is the binding part between the HTML (view) and the JavaScript (controller). The data entered in the form fields are automatically bound to the model object in the $scope and can be accessed in the controller for further processing.

AngularJS Form Controls

AngularJS offers several directives that can be used to control and manipulate the behavior of forms:

  1. ng-model: This directive binds the form field value to the property in the model object defined in $scope.
  2. ng-submit: This directive specifies a function to run when the form is submitted.
  3. ng-disabled: This directive binds the application data to the HTML disabled attribute.
  4. ng-change: This directive specifies an expression to evaluate when the user changes the input.

AngularJS Form Validation

AngularJS makes form validation simple and intuitive. It automatically checks for errors in the form fields and provides feedback to the user. This is done by adding certain properties to the form and its elements, which reflect the state of the form and its fields.

Here are the properties AngularJS adds to form and form control elements:

  1. $dirty: True if the user has interacted with the form field.
  2. $pristine: True if the user has not interacted with the form field.
  3. $valid: True if the field content is valid.
  4. $invalid: True if the field content is not valid.
  5. $error: An object that contains properties for each invalid rule on the form control.

By leveraging these properties, developers can create interactive feedback for users. You can use CSS classes that are automatically added and removed by AngularJS based on the state of the form or form elements. These classes include ng-valid, ng-invalid, ng-pristine, ng-dirty, etc.

Custom Validation

Beyond built-in validation options, AngularJS also allows developers to create their own custom validation directives. This adds flexibility to form validation, allowing developers to handle complex validation scenarios or to add validation that is specific to their application’s domain.


Form handling is a crucial part of any web application, and AngularJS provides a powerful, flexible way of managing and validating user input. Its two-way data binding keeps your model and view in sync at all times, while its extensive validation capabilities ensure that data entered by the user is correct before it’s processed. By understanding AngularJS form handling, developers can create forms that provide a smooth, interactive experience for users, thereby building more effective, user-friendly web applications.


Table of Contents

Related posts