๐งพ AngularJS Template-Driven Forms (2025 Guide)
๐งฒ Introduction โ Why Learn AngularJS Template-Driven Forms?
Template-driven forms are a key part of AngularJS and offer a simple, declarative way to create interactive, data-bound HTML forms. Without writing much JavaScript, developers can use AngularJS directives like ng-model, ng-submit, ng-required, and ng-pattern to build powerful and validated forms entirely from the HTML template.
These forms are perfect for:
- Quick form development
- Real-time validation feedback
- Two-way data binding between form fields and controller models
๐ฏ In this guide, youโll learn:
- How AngularJS template-driven forms work
- How to use
ng-model,ng-form, and validation directives - Real-world form examples
- Best practices and debugging tips
๐ What Are Template-Driven Forms in AngularJS?
Template-driven forms are built and managed using HTML directives. Most of the logic resides in the template, with minimal controller code.
Key features:
- Two-way data binding via
ng-model - Form status and validity tracking via
formName.$valid,formName.$dirty, etc. - Built-in validation using
ng-required,ng-minlength,ng-pattern, and more
โ Basic Example โ User Login Form
<form name="loginForm" ng-submit="login(user)">
<label>Email:</label>
<input type="email" name="email" ng-model="user.email" required>
<label>Password:</label>
<input type="password" name="password" ng-model="user.password" required minlength="6">
<button type="submit" ng-disabled="loginForm.$invalid">Login</button>
</form>
Controller:
$scope.user = {};
$scope.login = function(user) {
console.log("Login submitted:", user);
};
๐งพ Automatically validates input and disables the button if invalid.
๐งช Real-Time Form Validation Feedback
<form name="regForm" ng-submit="register(user)">
<input name="username" ng-model="user.username" required>
<span ng-show="regForm.username.$touched && regForm.username.$invalid">
Username is required.
</span>
<input type="email" name="email" ng-model="user.email" required>
<span ng-show="regForm.email.$dirty && regForm.email.$invalid">
Enter a valid email.
</span>
<button type="submit" ng-disabled="regForm.$invalid">Register</button>
</form>
๐งพ Shows error messages as soon as the field is touched or modified and invalid.
๐งฎ Common Validation Directives
| Directive | Description |
|---|---|
required | Marks a field as mandatory |
ng-minlength | Minimum character length |
ng-maxlength | Maximum character length |
ng-pattern | Validates input against a regex pattern |
type="email" | Built-in email validation |
type="number" | Number-specific validation |
๐ Accessing Form & Field State
AngularJS tracks form status using special $ properties:
| Property | Description |
|---|---|
$pristine | True if the field hasnโt been changed |
$dirty | True if the field has been modified |
$valid | True if field passes all validations |
$invalid | Opposite of $valid |
$touched | Field was focused and then blurred |
$untouched | Field hasn’t been blurred yet |
formName.$submitted | True if the form has been submitted |
Example:
<span ng-show="myForm.email.$touched && myForm.email.$invalid">
Email is invalid.
</span>
๐ฆ Grouping Forms with ng-form
You can group sections of a form using ng-form.
<form name="mainForm">
<ng-form name="subForm">
<input ng-model="data.value1" required>
<input ng-model="data.value2" required>
</ng-form>
</form>
๐งพ Helps isolate validation logic within sub-sections of a form.
๐ง Real-World Use Case โ Contact Form
<form name="contactForm" ng-submit="sendMessage(data)">
<input type="text" ng-model="data.name" required placeholder="Your Name">
<input type="email" ng-model="data.email" required placeholder="Your Email">
<textarea ng-model="data.message" required placeholder="Message"></textarea>
<button type="submit" ng-disabled="contactForm.$invalid">Send</button>
</form>
$scope.data = {};
$scope.sendMessage = function(data) {
alert("Message sent: " + data.message);
};
โ๏ธ Best Practices
โ๏ธ Always use name attribute in inputs for proper validation tracking
โ๏ธ Disable submit button using formName.$invalid
โ๏ธ Provide user feedback using $dirty, $touched, etc.
โ๏ธ Initialize model objects in controller
โ๏ธ Validate on the client side and again on the server side (for security)
๐ Summary โ Recap & Next Steps
AngularJS template-driven forms make it easy to build validated, interactive forms using just HTML and a controller. With built-in validation, two-way binding, and live feedback, they offer great flexibility and speed for form development.
๐ Key Takeaways:
- Use
ng-modelfor two-way data binding - Track validation with
$valid,$dirty, and$touched - Use
ng-submitand disable buttons conditionally - Group sub-forms using
ng-formif needed
โ๏ธ Real-world Relevance:
Commonly used in login forms, signup forms, feedback modules, order checkout, and admin dashboards.
โ FAQ โ AngularJS Template-Driven Forms
โ What is a template-driven form in AngularJS?
โ
A form created using HTML and AngularJS directives like ng-model, ng-submit, and validation attributes. Most logic resides in the template.
โ How does AngularJS validate forms?
โ
AngularJS uses built-in validation directives and tracks input state using properties like $valid, $dirty, and $touched.
โ Can I disable a form submit button if the form is invalid?
โ
Yes. Use ng-disabled="formName.$invalid" on the button.
โ What is ng-form used for?
โ
It allows grouping multiple inputs into a named sub-form for nested validation and logic separation.
Share Now :
