Mapping fields in Angular collectively

I'm currently working on implementing a modal, and I'm looking to link values from the formBuilder to a specific property.

Here's the snippet of code I'm working with:

submit(data?: any) {
    // THE FOLLOWING CODE WORKS, BUT IT'S NOT IDEAL IN TERMS OF QUALITY
    this.contact.firstName = this.contactForm.value.firstName;
    this.contact.firstName = this.contactForm.value.lastName;
    this.contact.firstName = this.contactForm.value.Email;
    this.contact.firstName = this.contactForm.value.PhoneNr;

    // WHAT I WOULD PREFER TO DO (ALTHOUGH 'CONTACT' CONTAINS MORE FIELDS THAN 'CONTACTFORM') 
    // this.contact = this.contactForm.value;

    this.onSubmit.emit(data);
}

The issue arises from the fact that the property contains more fields than the formBuilder. Is there a method to map the common fields while retaining the additional fields in 'Contact'?

Answer №1

Below is an example of something along these lines.

const updatedContact = {...this.existingContact, ...this.updatedContactForm.value};

Answer №2

Implement the spread syntax

this.data = {...this.formData};

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

I find myself hindered by TypeScript when trying to specify the accurate constraints for getUserMedia

I'm having difficulty getting a screen to stream within my Angular 5 Electron application. I am utilizing the desktopCapturer feature provided by Electron. Below is an excerpt of my code: loadCurrentScreensource() { desktopCapturer.getSources({ ...

Problem arises when combining string manipulation and piping operations

I have an HTML code within an Angular 2.0 template that looks like this: <span> Total Employee {{employeeCount> 0 ? '(' + employeeCount+ ')' : ''}} ></span> My customFormatter function is able to take a val ...

Guide to transferring a zip file from a server to a client in nodejs

After creating a zip file on the server side, I am trying to figure out how to transfer it to the client side in order to enable downloading using the saveAs() function and placing it within a new Blob() function. Any suggestions on how to accomplish this? ...

Guide on organizing a multi-dimensional array of objects based on property value using javascript?

I am faced with the challenge of sorting a multidimensional array based on values, but the selection is dependent on the parentID. This is my current array: const result = [ [{value: 123, parentID: 1}, {value: 'string123', parentID: 2}], [{ ...

Azure Function (.NET 7, v4) encountering Cross-Origin Resource Sharing problem

I am experiencing a CORS problem with my Azure function written in C# using .NET 7. It is being called from my Angular application. Interestingly, the same function works fine when called from Postman with the same request body as the Angular application. ...

Uninitialized Array Member in Angular 8

Can anyone explain why the code snippet below is printing "undefined"? I have created several objects and intended to display the corresponding images, but after iterating through them using ngfor, nothing appeared. To investigate, I logged the array and ...

What is preventing me from uploading the node_modules folder to my GitHub repository?

I've encountered an issue with uploading my Angular 6 project to GitHub using GitHub Desktop. Despite successfully uploading all files, the node_modules file is consistently missing. Upon downloading the project from GitHub and attempting to run it, ...

Upon completing the installation of the @angular/cli@latest node module, the ng file was unexpectedly missing

I'm currently working on Git Bash on my Windows 10 machine. The command I used was: npm install --save @angular/cli@latest Here is the result: + @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bad9d6d3fa8b899488 ...

Issue with Angular 6 Material2 mat-table MatRipple causing errors

When I try to use MatTable with a data source in Angular 6 and add sorting or pagination, I encounter the following error: ERROR Error: Uncaught (in promise): Error: Can't resolve all parameters for MatRipple: ([object Object], [object Object], [ob ...

The entire space should be filled with the background

My goal is to achieve the following while addressing some current issues: The background is currently limited to affecting only the container. I want it to span the entire area. There needs to be space between the cards and padding inside them. https://i ...

Communication breakdown between components in Angular is causing data to not be successfully transmitted

I've been attempting to transfer data between components using the @Input method. Strangely, there are no errors in the console, but the component I'm trying to pass the data from content to header, which is the Header component, isn't displ ...

ngModelChange doesn't trigger if the value is manually altered

Here is the scenario I am experiencing: //html <input (ngModelChange)="onSelection()" [(ngModel)]="selectedNode" > // in the ts file onSelection() { alert('changed'); } Typing something inside the input tri ...

Deployment error on Google App Engine for Angular Universal 13 and Angularfire 7

Upgrading my app from Angular 12 to Angular 13 has caused deployment issues on Google App Engine. The error message reads: ERROR: (gcloud.app.deploy) Error Response: [9] An internal error occurred while processing task /app-engine-flex/flex_await_healthy/ ...

Setting IDPs to an "enabled" state programmatically with AWS CDK is a powerful feature that allows for seamless management of

I have successfully set up Facebook and Google IDPs in my User Pool, but they remain in a 'disabled' state after running CDK deploy. I have to manually go into the UI and click on enabled for them to work as expected. How can I programmatically e ...

Challenge with the scope of 'this' in Typescript

Whenever I invoke the findFromList function from a component, it triggers this particular error message: ERROR TypeError: Cannot read property 'searchInArray' of undefined at push../src/app/shared/services/filter.service.ts.FilterService ...

Is there a way to retrieve the previously selected mat-tab in an Angular component, even after navigating to other components, without relying on localstorage or services?

Preserving mat-tab selection state between Angular components without relying on localStorage or a service Query: I'm currently developing an Angular application that features multiple components with mat-tab-groups. I would like to maintain the sele ...

What are the steps to send a Firebase cloud message using typescript?

I'm looking for guidance on how to send a Firebase Cloud Message through my Firebase Functions backend. I seem to be running into trouble with the payload and need help resolving it. Do I need an interface for the payload? Thank you in advance! Error ...

Error encountered when attempting to insert data into a PostgreSQL database using Node.js and Sequelize

I'm currently using the node sequelize library to handle data insertion in a postgress database. Below is the user model defined in the Users.ts file: export class User extends Sequelize.Model { public id!: number; public name: string; public ...

Practice with Angular - Testing asynchronous service calls

Encountering an issue with the code while attempting to check the DOM element. I have used ngIf with a service async call in HTML, and the sample code is provided below: <div class="" *ngIf="EmployeeService.employees$ | async"> <div class="h ...

There is no overload that matches this call in Next.js/Typescript

I encountered a type error stating that no overload matches this call. Overload 1 of 3, '(props: PolymorphicComponentProps<"web", FastOmit<Omit<AnchorHTMLAttributes, keyof InternalLinkProps> & InternalLinkProps & { ...; ...