How to create Forms in Angular JS

Last updated on May 31 2022
Santosh Prakash

Table of Contents

How to create Forms in Angular JS

AngularJS enriches form filling and validation. We can use ng-click to handle AngularJS click on button and use $dirty and $invalid flags to do the validations in seemless way. Use novalidate with a form declaration to disable any browser specific validation. Forms controls makes heavy use of Angular events. Let’s have a quick look on events first.

Events

AngularJS provides multiple events which can be associated with the HTML controls. For example ng-click is normally associated with button. Following are supported events in Angular JS.

  • ng-click
  • ng-dbl-click
  • ng-mousedown
  • ng-mouseup
  • ng-mouseenter
  • ng-mouseleave
  • ng-mousemove
  • ng-mouseover
  • ng-keydown
  • ng-keyup
  • ng-keypress
  • ng-change

ng-click

Reset data of a form using on-click directive of a button.

<input name = “firstname” type = “text” ng-model = “firstName” required>

<input name = “lastname” type = “text” ng-model = “lastName” required>

<input name = “email” type = “email” ng-model = “email” required>

<button ng-click = “reset()”>Reset</button>

 

<script>

function studentController($scope) {

$scope.reset = function() {

$scope.firstName = “Mahesh”;

$scope.lastName = “Parashar”;

$scope.email = “MaheshParashar@tecklearn.com”;

}

 

$scope.reset();

}

</script>

Validate data

Following can be used to track error.

  • $dirty − states that value has been changed.
  • $invalid − states that value entered is invalid.
  • $error − states the exact error.

Example

Following example will showcase all the above mentioned directives.

testAngularJS.htm

 

<html>

<head>

<title>Angular JS Forms</title>

<script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”></script>

 

<style>

table, th , td {

border: 1px solid grey;

border-collapse: collapse;

padding: 5px;

}

 

table tr:nth-child(odd) {

background-color: #f2f2f2;

}

 

table tr:nth-child(even) {

background-color: #ffffff;

}

</style>

 

</head>

<body>

 

<h2>AngularJS Sample Application</h2>

<div ng-app = “mainApp” ng-controller = “studentController”>

 

<form name = “studentForm” novalidate>

<table border = “0”>

<tr>

<td>Enter first name:</td>

<td><input name = “firstname” type = “text” ng-model = “firstName” required>

<span style = “color:red” ng-show = “studentForm.firstname.$dirty && studentForm.firstname.$invalid”>

<span ng-show = “studentForm.firstname.$error.required”>First Name is required.</span>

</span>

</td>

</tr>

 

<tr>

<td>Enter last name: </td>

<td><input name = “lastname”  type = “text” ng-model = “lastName” required>

<span style = “color:red” ng-show = “studentForm.lastname.$dirty && studentForm.lastname.$invalid”>

<span ng-show = “studentForm.lastname.$error.required”>Last Name is required.</span>

</span>

</td>

</tr>

 

<tr>

<td>Email: </td><td><input name = “email” type = “email” ng-model = “email” length = “100” required>

<span style = “color:red” ng-show = “studentForm.email.$dirty && studentForm.email.$invalid”>

<span ng-show = “studentForm.email.$error.required”>Email is required.</span>

<span ng-show = “studentForm.email.$error.email”>Invalid email address.</span>

</span>

</td>

</tr>

 

<tr>

<td>

<button ng-click = “reset()”>Reset</button>

</td>

<td>

<button ng-disabled = “studentForm.firstname.$dirty &&

studentForm.firstname.$invalid || studentForm.lastname.$dirty &&

studentForm.lastname.$invalid || studentForm.email.$dirty &&

studentForm.email.$invalid” ng-click=”submit()”>Submit</button>

</td>

</tr>

 

</table>

</form>

</div>

 

<script>

var mainApp = angular.module(“mainApp”, []);

 

mainApp.controller(‘studentController’, function($scope) {

$scope.reset = function() {

$scope.firstName = “Mahesh”;

$scope.lastName = “Parashar”;

$scope.email = “MaheshParashar@tecklearn.com”;

}

 

$scope.reset();

});

</script>

 

</body>

</html>

Output

Open textAngularJS.htm in a web browser. See the result.

So, this brings us to the end of blog. This Tecklearn ‘How to create Forms in Angular JS’ blog helps you with commonly asked questions if you are looking out for a job in Angular JS and Front-End Development. If you wish to learn Angular JS and build a career in Front-End Development domain, then check out our interactive, Angular JS Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/angular-js-training/

Angular JS Training

About the Course

Angular JS certification training is designed for developers who are responsible for building applications using AngularJS. Through a combination of presentations and hands-on practices, participants will explore Angular comprehensively and learn to build components, directives, template-driven forms, routing, etc. for complex data-centric (enterprise) applications. This Angular online training will update your knowledge and skills with the current Angular version to lift your career.

Why Should you take Angular JS Training?

  • The average salary for “Angular Developer” ranges from approximately $92,814 per year for a Developer to $113,069 per year for a Full Stack Developer (Indeed.com).
  • YouTube, Google, Cisco, Nike, Samsung, Microsoft, Forbes, BMW and many Fortune 500 companies are using Angular as their front-end development framework to deliver an engaging user experience.
  • Angular’s powerful cross-platform feature allows developers to build progressive web applications and native mobile applications.

What you will Learn in this Course?

Introduction to Angular

  • Introduction to Angular
  • Comparison between front-end tools
  • Angular Architecture
  • Building blocks of Angular
  • Angular Installation
  • Angular CLI
  • Angular CLI commands
  • Angular Modules

Angular Components and Data Binding

  • Working of Angular Applications
  • Angular App Bootstrapping
  • Angular Components
  • Creating A Component Through Angular CLI
  • Ways to specify selectors
  • Template and styles
  • Installing bootstrap to design application
  • Data Binding
  • Types of Data Binding
  • Component Interaction using @Input and @Output decorator
  • Angular Animations

TypeScript

  • What is TypeScript
  • Need of TypeScript
  • How to install TypeScript
  • Nodemon for monitoring changes
  • Interfaces in Class, String Templates, Maps,
  • Sets and Object Restructuring

Directives and Pipes in Angular

  • Angular Directives
  • @Component Directive
  • Structural Directives
  • Attribute Directives
  • Custom Directives
  • Pipes
  • Built-in Pipes
  • Chaining pipes
  • Custom pipes
  • Pipe Transform Interface & Transform Function
  • Pure and Impure pipes

Angular Services and Dependency Injection

  • Angular service
  • Need for a service
  • Dependency Injection
  • Creating a service
  • Hierarchical Injector
  • Injecting A Service into Another Service
  • Observables
  • RxJS Library
  • Angular’s Interaction with Backend
  • Parts of an Http Request
  • HttpClient

Forms in Angular

  • Forms in Angular
  • What are their functions
  • Advantages of Forms
  • Various Types of Forms
  • What is Angular Validation and Model driven approach

Angular Routing

  • Angular Routing
  • Angular Routing benefits and features
  • Building a single page application and updating it dynamically with Angular Routing
  • What is Parameter Routing
  • Router Lifecycle Hooks and Child Routes

Authentication and Security in Angular

  • What is Authentication?
  • Authentication and authorization
  • Types of Authentication
  • Where to store tokens?
  • JSON Web Tokens (JWT)
  • Authentication in Angular application
  • Security threats in web application

Testing Angular applications

  • Testing of Angular applications
  • Deployment of Angular Test Bed for testing on Angular framework
  • Setup of various tools for testing
  • Testing services in Angular Framework
  • E2E and Document Object Model testing

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "How to create Forms in Angular JS"

Leave a Message

Your email address will not be published. Required fields are marked *