How to add Buttons without Submit the Form in Angular?
Last Updated :
30 Jul, 2024
In Angular, when adding buttons to a form, their default behavior on click is to submit the form itself. However, there are certain situations in which it is necessary to create buttons for forms that provide some sort of functionality or trigger custom logic without triggering the default form submission behavior.
Steps to Configure the Angular Application
Step 1: Create an Angular application
Create an app using the Angular CLI to generate our base application structure, with routing and a CSS stylesheet.
ng new AngularApp
Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
Project Structure
Make the directory structure according to the following image:
.png)
Using the attribute type with the value button
- In this approach, the form is not submitted by clicking any button added with an attribute of the type button. This is because the behavior of the button is modified by specifying the type of the button as a button by type = "button" which prevents the submission of the form.
- The custom button is bound to the Angular form and any kind of pre-determined logic can be added with a click of the button as per the user's requirements.
- The submit button, on the other hand, submits the form and displays the submitted data accordingly with a ngIf directive to see if the form is submitted or not.
- In this manner, multiple buttons can be added to the form without submission of the form in Angular.
Example: Below is an example demonstrating the use of attribute type with the value of button in AngularJS. The "Increase Count" button is the custom button that is added to this form and on each click of the increase count button, the number is incremented by 1, whilst simultaneously preventing the submission of the Angular form.
HTML
<!--app.component.html-->
<h1>GeeksforGeeks Form</h1>
<div>
<form (ngSubmit)="onSubmit()">
<label for="name">Name</label>
<input type="text"
id="name"
name="name"
[(ngModel)]="formData.name" required>
<label for="email">Email</label>
<input type="email"
id="email"
name="email"
[(ngModel)]="formData.email" required>
<button type="submit">Submit</button>
<button type="button" (click)="onButtonClick()">
Increase Count
</button>
<div>
<pre style="margin: 1rem 0 1rem 0;">
Count: {{ clickCount }}
</pre>
</div>
<div *ngIf="displayFormData">
<pre style="margin-bottom: 1rem;">
Form has been submitted successfully!
</pre>
<pre style="font-weight: bold;">
Form Data:
</pre>
<pre> {{ formData.name }} </pre>
<pre> {{ formData.email }} </pre>
</div>
</form>
</div>
CSS
/*app.component.css*/
h1 {
text-align: center;
color: green;
}
div {
margin-left: 45%;
}
form div {
margin-top: 1rem;
margin-left: 0;
}
label {
display: block;
margin-bottom: 0.5rem;
}
input {
display: block;
margin-bottom: 1rem;
}
button {
cursor: pointer;
}
button[type="submit"] {
margin: 0 0.5rem 0.5rem 0;
}
JavaScript
// app.component.ts
import { Component }
from '@angular/core';
import { FormsModule }
from '@angular/forms';
import { CommonModule }
from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
standalone: true,
imports: [FormsModule, CommonModule]
})
export class AppComponent {
formData: any = {
name: "GeeksforGeeks",
email: "[email protected]"
};
displayFormData: boolean = false;
clickCount: number = 0;
onSubmit() {
// Handle form submission logic here
if (!this.displayFormData) {
this.update();
}
}
update() {
this.displayFormData = !this.displayFormData;
}
onButtonClick() {
this.clickCount++;
}
}
JavaScript
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule, provideClientHydration }
from '@angular/platform-browser';
import { AppRoutingModule }
from './app-routing.module';
import { AppComponent }
from './app.component';
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
AppComponent
],
providers: [
provideClientHydration()
],
bootstrap: [AppComponent]
})
export class AppModule { }
Output
Using $event.preventDefault() Method
- In this approach, the form is not submitted by clicking any button added with an $event.preventDefault() method. This is because the behavior of the button is modified by specifying the $event.preventDefault() method on the button as this method prevents the default behavior of the form which is the submission of the form.
- Here, also, the custom button will be bound to the Angular form(as done in the 1st approach) and any kind of pre-determined logic can be added with a click of the button as per the user's requirements. The submit button, on the other hand, submits the form and displays the submitted data accordingly with a ngIf directive to see if the form is submitted or not.
- In this manner, $event.preventDefault() can be used to add multiple buttons to the form without submission of the form in Angular.
Example: Below is an example demonstrating the use of the $event.preventDefault() method in AngularJS. The "Reset" button is the custom button that is added to this form and on each click of the reset button, the form values are cleared, whilst simultaneously preventing the submission of the Angular form.
HTML
<!-- app.component.html -->
<h1>GeeksforGeeks Form</h1>
<div>
<form (ngSubmit)="onSubmit()">
<label for="name">Name</label>
<input type="text"
id="name"
name="name"
[(ngModel)]="formData.name" required>
<label for="email">Email</label>
<input type="email"
id="email"
name="email"
[(ngModel)]="formData.email" required>
<button type="submit">Submit</button>
<button (click)="onButtonClick(); $event.preventDefault()">
Reset Form
</button>
<div *ngIf="displayFormData">
<pre style="margin-bottom: 1rem;">
Form has been submitted successfully!
</pre>
<pre style="font-weight: bold;">
Form Data:
</pre>
<pre> {{ formData.name }} </pre>
<pre> {{ formData.email }} </pre>
</div>
</form>
</div>
CSS
/* app.component.css */
h1 {
text-align: center;
color: green;
}
div {
margin-left: 45%;
}
form div {
margin-top: 1rem;
margin-left: 0;
}
label {
display: block;
margin-bottom: 0.5rem;
}
input {
display: block;
margin-bottom: 1rem;
}
button {
cursor: pointer;
}
button[type="submit"] {
margin: 0 0.5rem 0.5rem 0;
}
JavaScript
// app.component.ts
import { Component }
from '@angular/core';
import { FormsModule }
from '@angular/forms';
import { CommonModule }
from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
standalone: true,
imports: [FormsModule, CommonModule]
})
export class AppComponent {
formData: any = {
name: "GeeksforGeeks",
email: "[email protected]"
};
displayFormData: boolean = false;
onSubmit() {
// Handle form submission logic here
if (!this.displayFormData) {
this.update();
}
}
update() {
this.displayFormData = !this.displayFormData;
}
onButtonClick() {
this.formData.name = '';
this.formData.email = '';
}
}
JavaScript
// app.module.ts
import { NgModule }
from '@angular/core';
import { BrowserModule, provideClientHydration }
from '@angular/platform-browser';
import { AppRoutingModule }
from './app-routing.module';
import { AppComponent }
from './app.component';
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
AppComponent
],
providers: [
provideClientHydration()
],
bootstrap: [AppComponent]
})
export class AppModule { }
Output
Similar Reads
How to Toggle the Buttons & Submit a Form at the Same Time ?
In web development, it is common to come across situations where you need to toggle buttons and submit a form simultaneously This functionality is useful for users to interact with the form by enabling or disabling buttons while processing or submitting form data and integrating a button toggle with
2 min read
How to submit form on pressing Enter with Angular 9?
To submit a form in angular we have the following options: Create a button to submit the form.Assign a key to submit the formOr we can do both. In this tutorial, we will see how to use a specific key(Enter in this case) to submit a form. Approach: We can use angular keydown event to use Enter key as
2 min read
How to submit a form without using submit button using PHP ?
In this article, we will see how to submit the form without clicking the submit button, along with understanding the different ways to accomplish this task through implementation. The form can be submitted without using submit button by implementing a specific event attribute or by clicking the link
3 min read
How to use multiple submit buttons in an HTML form ?
Using multiple submit buttons in an HTML form allows different actions based on the button clicked. Each submit button can trigger a unique server-side or JavaScript process while sharing the same form fields, enabling various functionalities within a single form structure. Syntax<form action="/https/www.geeksforgeeks.org/D
2 min read
How to clear form after submission in Angular 2?
In Angular 2, they are two types of forms: Template-driven forms. Reactive forms. In template-driven forms, most of the content will be populated in .html file.In Reactive forms, most of the functionalities and content will be performed in .ts file. The main advantage of reactive forms is, we can cr
2 min read
How to assign and read values of two different submit buttons using AngularJS?
In this article, we will see how to assign and read the two different submit buttons' values in AngularJS. Usually, many websites provide relevant kinds of information and also supportive submit buttons like one or more than one also to proceed further. For instance, Users will be asked to fill out
4 min read
Angular PrimeNG Focus Trap Button with tabindex -1
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use the Focus Trap Button with tabindex -1 in Angular PrimeNG. We w
3 min read
How to change the font of HTML5 Canvas using a button in Angular.js?
In this article, we are going to learn about how to change the font of HTML5 Canvas using a button in AngularJS. With the help of a click font's property can change by the user whether it is font-size or font-style. Syntax: For font size (Change the font size accordingly): variable.fontSize = "100px
2 min read
How to get the value of the form in Angular ?
In this article, we are going to see how to get value from the form in Angular 10. We will use AbstractControl value property to get the form value in an object. Syntax: form.value Return Value: object: It is an object containing form value Approach: Create the Angular app to be usedIn app.component
1 min read
How to get the value of radio button using AngularJS ?
In this article, we will see how to get the value of the radio button using AngularJS, along with understanding the basic implementation through illustrations. The value of the checked radio button can be fetched with the help of the ng-model directive that helps to bind the data with the specific e
2 min read