AngularJS form.FormController
Last Updated :
02 Aug, 2023
AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. In this article, we’ll learn about the AngularJS form.FormController.
FormController
The FormController keeps track of all its controls and nested forms, as well as the state of the forms. It acts as a mediator between HTML forms and AngularJS controllers, providing an API to access form-related functionality and data. It allows you to track the state and validity of form elements, handle form submissions, implement custom validations, and more.
Methods for FormController
The FormController class has the following methods:
- $addControl(): It adds control to the form.
- $removeControl(): It removes a control from the form.
- $getControls(): It gets an array of all the controls in the form.
- $setDirty: It sets the form to a dirty state.
- $setPristine: It sets the form in its pristine state.
- $setUntouched: It sets the form to its untouched state.
- $setSubmitted: It sets the form to its $submitted state
- $setValidate(): It validates the form.
- $submit(): It submits the form.
- $commitViewValue(): It commits all form controls pending updates to the $modelValue.
- $rollbackViewValue(): It rolls backin all form controls pending updates to the $modelValue.
Properties of FormController:
- $valid: It indicates whether all the form controls are valid.
- $invalid: It indicates whether any of the form controls are invalid.
- $pristine: It indicates whether the form is in its initial (pristine) state, i.e., none of the form controls have been interacted with.
- $dirty: It indicates whether the form has been modified (dirty) by the user.
- $submitted: It indicates whether the form has been submitted.
- $error: It contains any validation errors associated with the form controls.
- $pending: It contains any pending asynchronous validators associated with the form controls.
We will understand the implementation of the different methods & properties associated with AngularJS FormController with the help of examples.
Using the ng-submit directive and $valid() Method
Syntax:
app.controller('FormController', function ($scope) {
$scope.formData = {}
$scope.submitForm = function () {
if ($scope.myForm.$valid) {
console.log('message!');
}
};
});
Example 1: In this example, we have used the ng-submit directive to trigger the form submission when the submit button is clicked. The form’s submit event is handled by the submitForm function, which checks if the form is valid before logging a success message to the console.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< title >
AngularJS Form Example
</ title >
< script src =
</ script >
</ head >
< body ng-app = "myApp"
ng-controller = "FormController" >
< h1 style = "color: green" >GeeksforGeeks</ h1 >
< h2 >AngularJS form.FormController</ h2 >
< form name = "myForm" ng-submit = "submitForm()" >
< label >Enter your name:</ label >
< input type = "text"
ng-model = "formData.name" required>
< br >
< label >Enter your email:</ label >
< input type = "email"
ng-model = "formData.email" required>
< br >
< button type = "submit" >Submit</ button >
</ form >
< script >
var app = angular.module('myApp', []);
app.controller('FormController', function ($scope) {
$scope.formData = {};
$scope.submitForm = function () {
if ($scope.myForm.$valid) {
console.log('Your form is submitted successfully!');
}
};
});
</ script >
</ body >
</ html >
|
Output:
.gif)
Using the setPristine() Method
Syntax:
$scope.submitForm = function () {
if ($scope.myForm.$valid) {
console.log('Form submitted successfully!');
$scope.myForm.$setPristine();
$scope.formData = {}; // Clear the form data
}
};
Example 2: In this example, we have used the setPristine() method that resets the form’s pristine state, removing any error styles and validation flags after successfully submitting the form.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< title >AngularJS Form Example</ title >
< script src =
</ script >
</ head >
< body ng-app = "myApp"
ng-controller = "FormController" >
< h1 style = "color: green" >
GeeksforGeeks
</ h1 >
< h2 >
AngularJS form.FormController
</ h2 >
< form name = "myForm"
ng-submit = "submitForm()" >
< label >Enter your name:</ label >
< input type = "text"
ng-model = "formData.name" required>
< br >
< label >Enter your email:</ label >
< input type = "email"
ng-model = "formData.email" required>
< br >
< button type = "submit" >Submit</ button >
</ form >
< script >
var app = angular.module('myApp', []);
app.controller('FormController', function ($scope) {
$scope.formData = {};
$scope.submitForm = function () {
if ($scope.myForm.$valid) {
console.log('Form submitted successfully!');
$scope.myForm.$setPristine();
// Clear the form data
$scope.formData = {};
}
};
});
</ script >
</ body >
</ html >
|
Output:
.gif)
Similar Reads
AngularJS Controllers
In this article, we will see the Controller in AngularJS along with knowing how Controller works, the concept of the Controller method & how the Controller can be implemented in an external. We will understand all these aspects with the help of their implementation & accordingly will its ill
3 min read
AngularJS $controller Service
AngularJS applications depend on a controller to control the flow of data through an AngularJS application. AngularJS architecture uses the MVC model i.e the Model View Controller. The model is responsible for maintaining the application data, the View for displaying data or some part of data to the
5 min read
Angular forms FormControlName Directive
In this article, we are going to see what is FormControlName in Angular 10 and how to use it. FormControlName is used to sync a FormControl in an existing FormGroup to a form control element by name. Syntax: <form [FormControlName] ="name"> Exported from: ReactiveFormsModule Selectors: [FormCo
1 min read
Angular forms FormControlDirective
In this article, we are going to see what is FormControlDirective in Angular 10 and how to use it. The FormControlDirective is used to synchronize a standalone FormControl instance to a form control element <form [FormControlDirective] ="name"> Exported from: ReactiveFormsModule Selectors: [Fo
1 min read
AngularJS ng-controller Directive
The ng-controller Directive in AngularJS is used to add a controller to the application. It can be used to add methods, functions, and variables that can be called on some event like click, etc to perform certain actions. Syntax: <element ng-controller="expression"> Contents... </element
2 min read
AngularJS | Forms
Forms are collection of controls that is input field, buttons, checkbox and these can be validated real time. As soon as a user of the form completes writing a field and moves to the next one it gets validated and suggests the user where he might have went wrong. So a form can be consisting of the m
3 min read
angular/router - NPM
Angular is a robust framework for building dynamic web applications. One of its core features is the Angular Router, a powerful module that allows developers to create single-page applications with navigation capabilities. This article will explain more about Angular Router. What is an Angular Route
2 min read
AngularJS Factory Method
AngularJS Factory Method makes the development process of AngularJS applications more robust. A factory is a simple function that allows us to add some logic to a created object and return the created object. The factory is also used to create/return a function in the form of reusable code which can
3 min read
AngularJS HTML DOM
The HTML DOM in AngularJS facilitates the directives that bind the application data with the attributes of HTML DOM elements. In this article, we will see such directives that help to bind the data to the HTML DOM element's attribute, along with their basic implementations through the illustrations.
2 min read
AngularJS Forms ngSubmit() Method
In this article, we are going to see what is ngSubmit method in Angular 10 and how to use it. The ngSubmit() method is called when the 'submit' event is triggered on the ngForm. Syntax: <form (ngSubmit)='method($event)'></form> Parameters: $event: the "submit" event object Approach: Crea
1 min read