πŸ“AngularJS Forms & User Input
Estimated reading: 4 minutes 277 views

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

DirectiveDescription
requiredMarks a field as mandatory
ng-minlengthMinimum character length
ng-maxlengthMaximum character length
ng-patternValidates 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:

PropertyDescription
$pristineTrue if the field hasn’t been changed
$dirtyTrue if the field has been modified
$validTrue if field passes all validations
$invalidOpposite of $valid
$touchedField was focused and then blurred
$untouchedField hasn’t been blurred yet
formName.$submittedTrue 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-model for two-way data binding
  • Track validation with $valid, $dirty, and $touched
  • Use ng-submit and disable buttons conditionally
  • Group sub-forms using ng-form if 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 :
Share

🧾 AngularJS Template-Driven Forms

Or Copy Link

CONTENTS
Scroll to Top