Submitting an Angular form in sections or all at once: A guide

Is there a way to submit a form in parts and together? I have a large form with multiple key combinations that need to be sent when editing fields separately. These parts are also utilized in various sections of the application. How can I make the forms independent yet cohesive, allowing for individual submissions as well as one unified submission? To illustrate, I've created two small examples. I'm looking to create a modular approach to avoid code repetition. However, I'm unsure how to divide the smaller forms into child components that can be edited and submitted individually with separate buttons, while still having an option to submit everything at once.

  constructor(private fb: FormBuilder) {
   this.form1 = this.fb.group({
     age: [20],
     name: ['asdas']
   });

   this.form2 = this.fb.group({
    adress: ['20'],
    city: ['Kiev']
  });

  this.form3 = this.fb.group({
    age: [20],
    name: ['asdas']
    adress: ['20'],
    city: ['Kiev']
 });
  }

  public onSubmit1(): void {
  // logic for form 1
  }
  public onSubmit2(): void {
    // logic for form 2
  }
  public onSubmit3(): void {
      // logic for form 3
  }

Answer №1

To consolidate the form values into a single object, you can utilize the submitAllForms() function located at the end of this code snippet.

constructor(private fb: FormBuilder) {
   this.form1 = this.fb.group({
        age: [20],
        name: ['asdas']
   });

   this.form2 = this.fb.group({
        adress: ['20'],
        city: ['Kiev']
   });

   this.form3 = this.fb.group({
        age: [20],
        name: ['asdas'],
        adress: ['20'],
        city: ['Kiev']
    });
}

...

public submitAllForms(): void {
    // a new object called "values" is created to combine all form values
    const values = {...this.form1.values, ...this.form2.values, ...this.form3.values}
}

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

Provide initial values to a custom TypeScript hook

As a new TypeScript user, I am trying to implement a hook called useForm to use in all forms. However, I am facing an issue with passing initial values using the code snippet below. Can someone help me troubleshoot this? interface IForm1 { name?: strin ...

Troubleshooting the issue of Angular Reactive Forms Select Option not properly selecting pre-defaulted objects

One issue I am facing is with a select option dropdown that fetches its options from the back-end through an API call and sets them. While trying to have a pre-selected option on page load, setting the value does not seem to be working for me. Even attempt ...

How to effectively merge DefaultTheme with styled-components in TypeScript?

I am facing an issue with integrating a module developed using styled-components that exports a theme. I want to merge this exported theme with the theme of my application. In my attempt in theme.ts, I have done the following: import { theme as idCheckThe ...

Unable to convert the value "Firefox" to the specified type 'Microsoft.VisualStudio.WebClient.Diagnostics.HtmlToolHost.PineZorro.DebugAdapterType'

I'm looking to switch from using Chrome to Firefox for my Angular project. I successfully installed the debug adapter from this link and it's working properly. However, when I attempted to replace launch.json in Vs2022, I encountered the followi ...

Testing server sent events with Angular solely using Karma-Jasmine

I am currently developing a web application using Angular for the frontend and Python for the backend. My implementation involves utilizing server-sent events (SSE) to stream data from the server to the user interface. While everything is functioning prope ...

Promise rejection: not as expected

I encountered an issue while using alert messages in my login menu: Runtime Error Uncaught (in promise): false Stack Error: Uncaught (in promise): false Here is the code snippet causing the problem: public login() { this.showLoading() this ...

Exploring the directories: bundles, lib, lib-esm, and iife

As some libraries/frameworks prepare the application for publishing, they create a specific folder structure within the 'dist' directory including folders such as 'bundles', 'lib', 'lib-esm', and 'iife'. T ...

The parent component remains visible in the Angular router

Within my appComponent, I have a layout consisting of a toolbar, footer, and main content. The main content utilizes the router-outlet directive structured as follows: <div class="h-100"> <app-toolbar></app-toolbar> < ...

My Angular-based todo application has encountered an error notification from the system

Every time I try to post something, the system responds with a 405 error message in the console. I'm not sure what caused this issue or how to resolve it. Alternatively, if I click the done button, the console displays a 500 error message. Here is t ...

Encountered an issue while attempting to assign a value to a property that is declared as [key in keyof T]

I am currently working on implementing a function that selects specific properties of an object. Here is the current function: const project = function<T>(object: T, projection: Projection<T>): Partial<T> { throw new Error("not imple ...

What is the best way to trigger an event within an Angular app using RxJS in version 10?

As I venture into the world of Angular10, I find myself experimenting with a Canvas and honing my skills in drawing on it. Let's refer to the object drawn on the canvas as a "Foobar" - my Angular10 code for drawing Foobars is coming along nicely. Util ...

Error: Gulp is using ts-node and returning 'void' instead of 'Task', but it cannot find the type 'Task'

Seeking assistance from experienced individuals in the realm of gulp.js and typescript - could someone provide guidance for a struggling newcomer? I am currently utilizing the most recent versions of all relevant tools (node, ts-node, gulp, ts, @types/gul ...

Is data binding not functioning properly in Angular 8?

This query has been raised multiple times in the past and I have reviewed all the suggested solutions. However, I am encountering a common issue - how to retrieve data from the parent component to the child component. I'm unsure of where I'm goin ...

Automate your Excel tasks with Office Scripts: Calculate the total of values in a column depending on the criteria in another column

As a newcomer to TypeScript, I have set a goal for today - to calculate the total sum of cell values in one column of an Excel file based on values from another column. In my Excel spreadsheet, the calendar weeks are listed in column U and their correspon ...

User instance does not function with the binding

When text is entered into the text box, the Angular form value changes but the userModel value remains the same , always displaying [Vino] as the userModel value. Below is the code from app.component.html, <form #userForm="ngForm"> {‌{userFor ...

Feeling perplexed about distinguishing between Modules and Components in Angular 2

Hey everyone, I'm just starting out with Angular2 and I have a question about the concepts of @NgModule and @Component: Are they completely different in terms of concept, or are they similar with the main difference being that @NgModule acts more li ...

Return to Angular 7 instead

Is there a way to revert back to angular 7 from angular 8? I had saved a branch in angular 7, but once I did npm install on the angular 8 branch, I started getting a plethora of errors with every build. Even after trying npm install again on the angular ...

Error: Unable to access $rootScope in the http interceptor response function

I have set up an interceptor to display an ajax spinner while loading. interface IInterceptorScope extends angular.IRootScopeService { loading: number; } export class Interceptor { public static Factory($q: angular.IQService, $ro ...

Implementing tailwindcss styles in a typescript interface with reactjs

In my code, I have a file named types.ts that defines an interface called CustomCardProps. I pass this interface to the CustomCard component within the home.tsx file. Additionally, I have a folder named constant with an index.ts file where I store values o ...

How can I clear the div styling once the onDismiss handler has been triggered

Seeking assistance with resetting a div on a Modal after it has been closed. The issue I am facing with my pop-up is that the div retains the previous styling of display: none instead of reverting to display: flex. I have searched for a solution without su ...