Deactivate the button while the form is being submitted

I need a way to prevent users from clicking the submit button multiple times while the form is being processed by the server. Below is the solution I have come up with:

clear() {
    this.count++

    this.formGroup.get('name').reset(null);
     ..........
    if (this.count === 2) {
        this.count = 1;
        document.querySelector('.create-btn').setAttribute('disabled', '');
    }

    }

    isDisabled() {
    document.querySelector('.create-btn').removeAttribute("disabled");
}

Here is the HTML code:

<form [formGroup]="formGroup" (ngSubmit)="submit()" >
    <div class="form-group" [ngClass]="errorClasses('name')">
        <label>Name</label>
        <input type="text" class="form-control name" formControlName="name" (click)="isDisabled()">
        <div class="help-block form-text with-errors form-control-feedback" *ngIf="controlHasErrors('name')">
            {{controlValidateMessage('name')}}
        </div>
    </div>

    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <button type="button" class="btn btn-block"
                    [ngClass]="{'btn-success': formGroup.get('enabled').value, 'btn-light': !formGroup.get('enabled').value}"
                    (click)="selectTnx('enabled')">
                        <i class="fa fa-check mr-2"*ngIf="formGroup.get('enabled').value"></i>Enabled
                </button>
            </div>
        </div>
    </div>
</form>

Are there any other methods to disable the submit button while the form is being submitted?

Answer №1

When it comes to styling, you have the freedom to go for something complex or keep it simple. For instance:

HTML
<input [disabled]="sendDisabled" type="button" value="Send">
...
Angular-js-class
sendDisabled = false

Answer №2

To disable the button, you can simply set the button to have the attribute disabled.

<button type="button" class="btn btn-block" [disabled] ="btnDisabled"
[ngClass]="{'btn-success': formGroup.get('enabled').value, 'btn-light': !formGroup.get('enabled').value}"
(click)="selectTnx('enabled')"><i class="fa fa-check mr-2"
*ngIf="formGroup.get('enabled').value"></i>Enabled</button>

When the button is clicked, you can simply set btnDisabled = true in your component. This will disable the button and apply the necessary styles.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The error message states: "There is no property named 'controls' on the type 'AbstractControl<any, any>'"

I have encountered an issue with my reactive form on Angular versions above 13. Despite using FormGroup and FormArray with proper typecasting, I am still facing errors. How can I resolve this? Working code in Angular 11: here Error shown in Angular 15: h ...

Highcharts - running out of space for tooltips – what's the solution? Show only the tooltip for the series being hovered over, or adjust the layout to accommodate all tooltips

I am facing an issue where not all of the tooltips on my chart are displayed when hovering, as shown in the image provided. Specifically, the tooltip for the green series is missing. https://i.sstatic.net/QcbWV.png Upon researching, I discovered that Hig ...

Display the HTML string as regular text in an Angular application

I'm working on a blog project with Angular. I need to display HTML content retrieved from the database as plain text for each blog post preview. The HTML formatting was done with ngx-quill. While I can render the rich text using <quill-view [conte ...

The combination of Angular2, TypeScript, and moment.js is lacking in terms of available locales (only 'en' is supported)

Currently, I am following a tutorial in a book to grasp the concepts of TypeScript and AngularJS 2.0:(Become_a_Ninja_with_Angular2). In one section, the tutorial walks through creating a custom Pipe and demonstrates an implementation using moment.js. To ...

Converting a file into a string using Angular and TypeScript (byte by byte)

I am looking to upload a file and send it as type $byte to the endpoint using the POST method My approach involves converting the file to base64, and then from there to byte. I couldn't find a direct way to convert it to byte, so my reasoning may be ...

Utilizing Generic Types in React TypeScript Functional Components

I'm currently developing a TypeScript React component that includes generic type props, so I define the React component as: export interface MyCompProps<T> { items: T[]; labelFunction: (T) => string; iconFunction: (T) => JSX.Element; ...

Can TypeScript support promise chaining in a functional way?

It appears that in the realm of JavaScript, one has the capability to execute: function extendPromise(promise) { return promise.then(new Promise(() => {})); } However, when incorporating types into the mix, such as function extendTypeScriptPromis ...

Incorporating an external module into your Angular application for local use

I currently have two projects: my-components, which is an Angular library, and my-showcase, an Angular app. Whenever I make changes or add a new component to my library, I commit it to a private git repository and publish it to a private npm registry. This ...

Angular Reactive Forms may not properly update other inputs when binding a control to multiple inputs

While working with reactive forms, I encountered an issue where accessing the same control in multiple inputs seemed to result in one-way data binding (input-to-model). If I make edits in one input, it updates the model correctly but does not refresh the o ...

When submitting in an Angular mat-dialog, the page should refresh without closing the dialog

I recently started working with Angular and had to retrieve data from the database to populate a user grid. I successfully completed that task and then moved on to using MatDialog for creating new users. After fixing the creation services and linking them ...

Tips for showing both label and value on a pie slice in Apex charts

I am currently utilizing apex chart within an angular application to showcase charts. I am specifically focusing on a pie chart and aiming to customize it by displaying labels on the values within each slice of the pie, similar to what is shown in the atta ...

Changing the generic type's constraint type in TypeScript to have more flexibility

I have developed a utility type named DataType that takes in a parameter T restricted to the type keyof MyObject. When the key exists in MyObject, DataType will return the property type from MyObject; otherwise, it will simply return T. interface MyObject ...

Align a single item to the right in PrimeNG p-menubar

I am currently utilizing PrimeNG 8.1.1 and I am looking for a way to align one of the items to the right side (specifically the submenu options of logout and profile). Does anyone have any suggestions? this._menuItems = [ { labe ...

Ways to resolve the issue of 'message' property lacking an initializer in TypeScript without suppressing errors

Currently, in the process of using TypeScript and Sequelize to create a model within Node.js. import { Table, Column, Model, AllowNull } from 'sequelize-typescript'; @Table class Person extends Model { @Column @AllowNull(false) name: strin ...

Encountered an issue when trying to deserialize the current JSON object while submitting relational data

I have encountered a problem while trying to add a worker to a job, specifically when dealing with the ModelState check. Here are the steps I've taken: Filling out the form Upon submitting the form, I checked the console and found: Name = "test", ...

Ways to cancel a subscription once a specific parameter or value is met following a service/store interaction

I am working with a service that provides a changing object over time. I need to unsubscribe from this service once the object contains a specific property or later when the property reaches a certain value. In situations like these, I typically rely on t ...

Exploring the Angular 2 Framework by Streaming Data from a RESTful API

Is it possible to easily handle a continuously streaming data feed in angular2? I am working on a chat application with a server written in go and a client implemented in angular2. To enable real-time updates, I have set up an endpoint that maintains the c ...

What is the best way to enable code sharing between two TypeScript projects housed within the same repository?

Our project has the following structure: api (dir) -- package.json -- tsconfig.json -- src (dir) -- client (dir) ---- package.json ---- tsconfig.json ---- src (dir) The "client" directory houses a create-react-app project that proxies to the API d ...

What is the best way to access a web.config key using Typescript?

Is there a way to retrieve key values in an Angular 2 application using TypeScript? add key="localPath" value="http://localhost:618/" add key="serverPath" value="http://api.azure.net/" I am looking to access the values of "localpath" and "serverpath" in ...

The NgZone reference error caused the prerendering to fail

I am facing challenges with the implementation of NgZones. Despite defining NgZone, I keep encountering this error: "NodeInvocationException: Prerendering failed because of error: ReferenceError: NgZone is not defined" Below is my app.error-handle.ts file ...