Learn how to achieve transparency or disable the div element while displaying the spinner component in Angular 2

Building a form with various fields and a submit button, I have set it up so that the submit button is active only when all input fields are valid. When the user clicks on the submit button, an API call is triggered and in the meantime, a loading spinner is displayed. Below is the code snippet:

HTML:

<div class="col-md-10 col-lg-5" id="mainDiv">
    <form  [formGroup]="userRegisForm">
                <fieldset>
                    <div class="m-l-1">
                        <div class="form-group required">
                            <label for="name" class="col-md-3 control-label">Name:</label>
                            <div class="col-md-6 col-lg-7">
                                <input type="text" class="form-control" id="name" name="Name" formControlName="name"
                                    aria-required="true" required>
                            </div>
                        </div>
                    </div>
                    <div class="m-l-1">
                        <div class="form-group required">
                            <label for="email" class="col-md-3 control-label">email:</label>
                            <div class="col-md-6 col-lg-7">
                                <input type="text" class="form-control" id="email" name="email" formControlName="email"
                                    aria-required="true" required>
                            </div>
                        </div>
                    </div>                    
                    <div class="m-l-2">
                        <div class="form-group">
                            <div class="">
                                <button type="submit"  (click)="submit()" [disabled]="!userRegisForm.valid">Submit</button>
                            </div>
                        </div>
                    </div>
                    <my-spinner [isRunning]="isRequesting"></my-spinner>
                </fieldset>
   </form>
</div>

Component:

'use strict';

import {Component} from 'angular2/core';

import {SpinnerComponent} from '../spinner/spinner';  
import {ApiService} from '../../services/apiService';

@Component({
    selector: 'my-sample-view',
    directives: [SpinnerComponent],
    template: '<my-spinner [isRunning]="isRequesting"></my-spinner>',
})
export class SampleViewComponent {  
    public isRequesting: boolean;
    public items: Array<any>;

    constructor(private apiService: ApiService) {
        this.submit();
    }

    public submit(): void {
        this.isRequesting = true;
        this.apiService.sampleHttpGetRequest()
            .subscribe(
                result => this.items = result, 
                () => this.stopRefreshing(),
                () => this.stopRefreshing());
    }

    private stopRefreshing() {
        this.isRequesting = false;
    }
}

I discovered this implementation idea from the following URL:

While the spinner displays during the request process, I noticed that the background remains active. To enhance user experience, I aim to disable all form fields, including the submit button, when the spinner is active.

Answer №1

Utilizing HTML5, you can easily deactivate all input fields within a fieldset by setting disabled attribute

To achieve this functionality...

<form>
<fieldset [disabled]="booleanFormDisabled">
    your desired input fields
</fieldset>
</form>

Alternatively, you can place a semi-transparent overlay on top of the form to prevent interaction with its elements.

Answer №2

If you're looking to disable specific elements, consider enclosing them in a div or section with an opaque class:

<section [class.<your class>]="isRequesting">

</section>

You can also apply this approach to disabling a form by wrapping it in a fieldset element:

<fieldset [disabled]="isRequesting">

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 parameter type must be a string, but the argument can be a string, an array of strings, a ParsedQs object, or an array of ParsedQs objects

Still learning when it comes to handling errors. I encountered a (Type 'undefined' is not assignable to type 'string') error in my code Update: I added the entire page of code for better understanding of the issue. type AuthClient = C ...

Vue + TypeScript prop type issue: "'Foo' is intended as a type, but is being treated as a value in this context."

As a newcomer to TypeScript and the Vue Composition API, I encountered an error that left me puzzled: I have a component that requires an api variable as a prop, which should be of type AxiosInstance: export default defineComponent({ props: { api: A ...

I'm puzzled as to why I have to encode the path parameter twice in order for the REST call to properly handle special characters

I have a challenge with a REST call that fetches a product's ingredients using the product name as the path variable. I allow for any character to be used, and I know I need to use encodeURIComponent(name) to encode characters such as "/" to prevent m ...

Display the following information as you iterate through an array in TypeScript within a React component

Currently, I am working on adding data to two separate arrays in a React TypeScript project. const [deviceNames, setDeviceNames] = useState<Array<string>>([]) const [serialNumbers, setSerialNumbers] = useState<Array<string>>([]) ...

The data type 'FetchData[]' cannot be assigned to type 'string' in Ionic React with Typescript

How can I store the response.body, which is a base64 string of a file, into a String variable? https://i.sstatic.net/JTkU7C2C.png fetchCustom(store.apiURL, { trtyp: 'file_download', seskey: store.userSessionKey, ...

Utilizing typed arrays within generic functions

Within a library, there exists a helper function designed to work with arrays of any type. The next step is to expand its functionality to also accommodate typed arrays. However, the challenge lies in the absence of a common base class for typed arrays or ...

Typescript threw an error: Zod was anticipating a string, but instead it got

I am encountering a Zod error multiple times while attempting to submit my req.body data to the Prisma ORM using Insomnia: ZodError: [ { "code": "invalid_type", "expected": "string", "received" ...

What is the best way to add CSS classes to a different component in AngularDart?

Imagine a straightforward framework for displaying popups: @Component( selector: 'popup-host', template: ''' <div class="popup-container"> <ng-template #popupRef></ng-template> </div> ...

What is the correct method for incorporating validators into an already existing set within a Custom Form Control?

I'm questioning the validity of my solution. My application utilizes Reactive Form, and for my CustomFormControl (which implements ControlValueAccessor), I've included a validator myControl: ["", Validators.required]. This validator is only requi ...

Emphasize x-axis heading in a Highcharts graph

In my Highcharts bar graph, I am looking for a way to dynamically highlight the x-axis label of a specific bar based on an external event trigger. This event is not a standard click interaction within the Highcharts graph. Currently, I have been able to r ...

Prompt user to save changes or cancel before closing modal (if closed by pressing ESC or clicking the backdrop)

When I manually close the modal, everything works fine. I just create a prompt and only call the BsModalRef.hide() method when the prompt (sweetalert) is closed. However, when the modal is closed using the ESC key or click-outside events provided by Boots ...

Utilizing React with .NET 8.0 and Minimal API, the front end is configured to send HTTP requests to its own specific port rather than the

Transitioning from working with react projects on .NET 3.1 to creating a new project on .NET 8.0 has been a challenging and confusing experience for me. When I attempted to access a newly created controller method, I encountered the error 404 Not Found. It ...

Tips for retrieving the Solana unix_timestamp on the front-end using JavaScript

Solana Rust smart contracts have access to solana_program::clock::Clock::get()?.unix_timestamp which is seconds from epoch (midnight Jan 1st 1970 GMT) but has a significant drift from any real-world time-zone as a result of Solana's processing delays ...

Encountering TypeScript errors when utilizing it to type-check JavaScript code that includes React components

My JavaScript code uses TypeScript for type checking with docstring type annotations. Everything was working fine until I introduced React into the mix. When I write my components as classes, like this: export default class MyComponent extends React.Compo ...

Tips for displaying multiple card items within an Angular Bootstrap Carousel

I've been experimenting with this carousel feature. I'm looking for a carousel similar to the multiple-item one showcased here. While attempting to follow this tutorial, my website encountered several errors and broke entirely. Here's wha ...

An error is being encountered with web-animations-js on IE 9, showing the following message: "SCRIPT5007: Object expected; web-animations.min.js (15

Currently, I am utilizing Angular 5. Below are the steps I followed: Firstly, added web-animations-js as a dependency : npm install web-animations-js Then, uncommented the following line in polyfils.ts import 'web-animations-js'; // Run npm ...

What is the data type returned by mapping an array in TypeScript?

I am working on a function that handles sorting columns export const handleSortableColumns = (headerKeys: string[], sortKeys: object): string[] => { if (!headerKeys) return []; return headerKeys.map((key: string): any => sortKeys[key] || null); ...

Information will only be displayed after the button has been double-clicked

After a month of experimenting with Angular and being relatively new to web development, I've encountered an issue that I hope someone can help me with. In my simple Angular application, I have a button called roomSearch() which sends form data to an ...

Using ng-bootstrap's ngbDatepicker will mark the form as invalid when transitioning from a date value to null

In my Angular application, I am trying to implement bootstrap's ngbDatepicker for a non-required date field. However, when the field contains a date and is then set to null, it causes the form to be marked as invalid. The following code demonstrates ...

Angular: Leveraging Injector to pass data in ngComponentOutlet

I am working on dynamically creating and displaying a component, and I need to pass some data into it so that it can determine what to show. Below is the code snippet: HTML section: <div class="basicContainer"> <div class="projectsTreeContain ...