Enabling All Angular Reactive Form Controls Simultaneously: A Step-by-Step Guide

I am facing an issue with a reactive form containing 35 fields. Initially, all controls need to be disabled when the page loads. However, upon clicking the 'Edit' button, I want all 35 controls to become enabled. Here is my attempted solution:

HTML

<form ...>
    Email input field...
    Contact input field...
    33 other fields...
</form>

<button (click)="allowFormUpdate()">Edit</button>

TS

formUpdateStatus: boolean = true; // VARIABLE TO INITIALLY DISABLE THE FIELDS
myReactiveForm;

allowFormUpdate() {
    this.formUpdateStatus = false; // NOT WORKING
    this.myReactiveForm.controls['email'].enable();
    this.myReactiveForm.controls['contact'].enable();
    this.myReactiveForm.controls['firstName'].enable();
    ...
    // Code being repeated
}

ngOnInit(): void {
    ...
    this.myReactiveForm= new FormGroup({
      email: new FormControl({value: '...', disabled: this.formUpdateStatus}),
      contact: new FormControl({value: '...', disabled: this.formUpdateStatus}),
      firstname: new FormControl({value: '...', disabled: this.formUpdateStatus}),
      ...     

Upon clicking the 'Edit' button, although allowFormUpdate() method was triggered, I had to enable each control individually as setting this.formUpdateStatus = false; did not work. Is there a way to enable all fields at once? Please assist me with this.

Answer №1

If you want to activate all form controls within the FormGroup, you can do so with this code:

this.myReactiveForm.enable();

Similarly, if you need to deactivate all form controls by turning off the FormGroup instead of disabling each individual FormControl with the disabled attribute.

this.formUpdateStatus && this.myReactiveForm.disable();

Check out the demonstration on StackBlitz

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

Error in unit testing: Trying to access property 'pipe' of an undefined variable results in a TypeError

Recently, I've been faced with the challenge of writing a unit test for one of the functions in my component. The specific function in question is called 'setup', which internally calls another function named 'additionalSetup'. In ...

What is the best way to restrict a Generic type within a Typescript Function Interface?

Imagine having the following interface definitions: interface SomeInterface {a: string; b: number} interface SomeFunction<T> {(arg: T) :T} The usage of the function interface can be demonstrated like this: const myFun: SomeFunction<string> = a ...

How does Angular2 indicate the modification in a segment of Component?

ReactJs utilizes virtual DOM for rendering changes, whereas Angular2 does not have a virtual DOM. However, Angular2 is reactive similar to ReactJS. In Angular2, with any change, the whole component does not need to be modified, only the portion of the co ...

``There seems to be an issue with AngularCli loading images

Just a quick inquiry: I have an Angular2 application that I am serving using the command ng serve, and it is rendering properly. However, I am encountering an issue: The CSS files are loading correctly, but the images are not. For instance, if I place ...

What is the best way to incorporate the "child_process" node module into an Angular application?

Trying to execute a shell script in my Angular application has been quite the challenge. I came across the "child process" node library that might be able to help me with this task. However, every attempt to import the library led me to the error message: ...

Angular MDBootstrap formatting disappears upon refresh of the page

I am currently utilizing the MDBootstrap package for Angular in my project, specifically focusing on styling buttons within a page I am developing. Upon initial loading of the page, the button styles are successfully applied. However, upon reloading the p ...

Collaborate on code for a cross-platform mobile application and a traditional desktop web application

I have a vision to create a cutting-edge app that can be utilized as both a hybrid mobile application and a desktop web application. For the most part, the logic and user interface will remain consistent across both versions. However, there are a few key ...

Can we streamline a generic overloaded function's signature to make it more concise?

I have developed a streamlined Axios wrapper function that integrates zod-parsing and presents a discriminated union for improved error handling. While the implementation successfully maintains the default behavior of Axios to throw errors in certain cas ...

Experiencing a useContext error when implementing MDX with NextJS 13

I am currently working on integrating mdx files into Next.js 13. After completing all necessary configurations in next.config and creating the file structure, I have the following path within the app folder: > docs > components > accordion > pa ...

The compiler mistakenly infers an incorrect type while utilizing a default value for a discriminated union type

I am currently working on a simplified component: export type BaseProps = { size?: 'xs' | 'sm' | 'md' | 'lg'; } type ButtonAsButtonProps = Omit<React.ComponentPropsWithoutRef<'button'>, ' ...

Converting API response into a class instance using `class-transformer` in TypeScript: A step-by-step guide

When working with TypeScript, I have a regular method called Request(method: HttpMethod, url: string, ...) that is used for calling APIs. Now, my goal is to convert the response from this API request into an instance of a class using class-transformer (or ...

Tips on retrieving the dimensions of a general map in typescript version 2.8.3

I am currently developing an application that is built on Angular 2.4 and Typescript 2.8.3. In this project, I have created a generic map structure and need to determine its size after adding or removing elements. let map = new Map<String, String>() ...

Error Encountered: White screen issue affecting Ionic 6 / Angular 14 app running on Android 6.1 device without Google Mobile

After successfully deploying my new Ionic 6 / Android 14 app on multiple devices, I encountered a problem with one specific device - a Zebra ET50 running Android 6.1 without Google Mobile Services. The app fails to load and only shows a white screen. I no ...

When modifying the state of an array within a component, certain values may be overwritten and lost in the process

Currently, I'm faced with the challenge of ensuring that a component displays a loading screen until all images have completed loading. This is necessary because there are approximately 25 images that must finish loading before the page can be display ...

What is the best way to exhibit information from a get request within an option tag?

My GET request is fetching data from a REST API, and this is the response I receive: { "listCustomFields": [ { "configurationType": null, "errorDetails": null, "fieldId" ...

When I use React Axios to make a request, I am unable to see the cookie

While I understand this topic has been covered extensively, I have exhausted all possible solutions and still can't resolve the issue. Here is my code for handling a login request in React using Axios: async login(email: string, password: string) { ...

Angular 7 library failing to transfer assets to dist folder

Upon generating a new library using Angular CLI v7, I proceeded to create a folder called assets within the src directory. This resulted in the following folder structure: | - src | - - lib | - - - assets However, when running the command ng build --proj ...

Ways to access the current route parameters when a component is not directly loaded by the router and the route changes

I am currently working on extracting parameters from a route that follows the pattern "/categories/:cat1/:cat2/browse" Retrieving the parameters in the component that is loaded by the route appears to be functioning correctly. However, I am facing an issu ...

Encountering a CORS policy error when attempting to connect to a Node.js service

Having issues connecting my Nodejs service (deployed on a server) from Angular (running on my local machine) due to CORS policy error. Access to XMLHttpRequest at 'http://xxxxxxx.xxxx.net:3500/api/get' from origin 'http://localhost:3000& ...

Improving the management of user input in Lit components

I am seeking a more efficient method to manage all inputs within my lit component while minimizing the code usage. I am also employing Typescript in this process, utilizing @change=${this.handleInput} for all input fields. Below is an example of how the ha ...