Tips on clearing and updating the Edit Modal dialog popup form with fresh data

This code snippet represents my Edit button functionality.

The issue I am facing is that I cannot populate my Form with the correct data from another component. Even when I click the (Edit) button, it retrieves different data but fails to update my form, resulting in it being stuck with the old data. I require assistance in resolving this issue.

public editUser(user: User): void {
   var id = user.Id;
   localStorage.removeItem("Id");
   localStorage.setItem("Id", id.toString());
   this.userComponent.openModal("custom-modal",user);
};

Below is a section of code from a separate component related to the Edit button:

ngOnInit() {
var id = localStorage.getItem('Id');
localStorage.removeItem('Id');

if (id) {
  this.userService.getById(id).subscribe(res => this.item = res);
  console.log(id);
}

this.myGroup = new FormGroup({
    Id: new FormControl(),
    UserName: new FormControl(),
    Password: new FormControl(),
    FirstName: new FormControl(),
    LastName: new FormControl(),
    Email: new FormControl()
  });
}

View the output in the browser console after each click on the button:

https://i.sstatic.net/Eilyj.jpg

Answer №1

To modify information in a dynamic form, you must utilize the patchValue() method.

this.myGroup.patchValue({'FirstName':'John Doe'});

This approach has successfully updated my data.

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

Reactjs Promise left hanging in limbo

How can I resolve the pending status of my promise? I have a modal with a form submit in it, where I am trying to retrieve the base64 string of a CSV file. While my code seems to be returning the desired result, it remains stuck in a pending state. c ...

My route seems to be malfunctioning, can someone please help me troubleshoot the

There seems to be an issue with my route in the app-routing.module.ts file because I encounter errors when I visit this page: However, everything works fine when I go here: import { NgModule } from '@angular/core'; import { Routes, Router ...

The conundrum of Content-Type in Angular 8's HttpClient Post request

Seeking assistance with POST request to backend API! I'm encountering a 415 status code due to the content-type being sent as "text/plain" when the endpoint expects application/json. Interestingly, the POST works in PostMan (see screenshot below). I ...

insert information into a fixed-size array using JavaScript

I am attempting to use array.push within a for loop in my TypeScript code: var rows = [ { id: '1', category: 'Snow', value: 'Jon', cheapSource: '35', cheapPrice: '35', amazonSource ...

Adjust the color of the text in Ionic based on the condition

My current solution involves highlighting text in a small slider after a user tap, but it feels very makeshift. <ion-slide *ngFor="let loopValue of values"> <div *ngIf="viewValue == loopValue"> <b> {{loopValue}} </b> ...

Issue: Interface not properly implemented by the service

I have created an interface for my angular service implementation. One of the methods in the service returns an observable array, and I am trying to define that signature in the interface. Here's what I have so far: import {Observable} from 'rxj ...

Exploring Angular 2+: Asynchronous Testing with setTimeout

I have a question regarding my testing process. I am using Angular 6, karma, and jasmine. Here is the test I have written: it(`my test`, async(() => { console.log('### start test'); fixture.detectChanges(); // calling a method wi ...

The module 'primeng/utils' does not contain the export 'FilterUtils'

Recently, I upgraded Angular to version 15 and encountered an error: The module ""primeng/utils"" does not have an exported member called 'FilterUtils'. Is there a way to implement filtering similar to this without making changes to ...

Angular: Dynamically changing checkbox's status from parent

I'm in the process of developing a switcher component that can be reused. The key requirement is that the state of the switch should only change after an API call is made at the parent component level. If the API call is successful, then the state cha ...

Step-by-step guide on bypassing Content Security Policy with JavaScript

I have Content Security Policy enabled for security purposes in my current project, but I need to disable it for certain JavaScript files. Can this be done? I am trying to make API calls from my JavaScript files in order to retrieve results. ...

Angular component failing to refresh data upon service response

Within my Angular component, I have integrated badges onto certain icons. These badge numbers are fetched from an api upon entering the page, utilizing ionViewWillEnter(). Once the api response is received, the outcome is stored in a local variable, which ...

The MdIcon in Angular 2 Material that functions properly in one component is causing issues in another

After upgrading from ng2 rc4 with material2 alpha6 to ng2 rc5 with material 2 alpha7-2, I encountered a new error related to the usage of <md-icon> which was previously working fine. The error message that appears now is Observable_1.Observable.thro ...

Is there a way to ensure that the content of my modal dialog only displays once when it is opened?

While working with Chakra UI, I encountered a unique issue that I hadn't faced before with Material UI. The problem arises when using the Chakra UI modal dialog - all components inside it get rendered twice upon opening. Despite attempting to disable ...

:host-selector for Angular Material dialog

I am currently working with a dialog component provided by angular-material and I need to customize the appearance of the popup dialog. I am aware that there is some support for styling through the component generation: let dialogRef = dialog.open(MyDi ...

Interactive website built on Angular 16 offering advanced search and result display functionalities, along with options to edit and update data

Seeking guidance from experienced Angular developers as I am relatively new to the framework. Any tips or advice would be greatly appreciated. Project Overview: Front-end development using Angular, minimal focus on Back-end (C#) for now. https://i.sstati ...

The field '_id' is not present in the type Pick

I'm working on a project using Next.js and attempting to query MongoDB with TypeScript and mongoose, but I keep encountering a type error. types.d.ts type dbPost = { _id: string user: { uid: string name: string avatar: string } po ...

The interface "App" is not properly implemented by the class "FirebaseApp" causing an error

My attempt to set up AngularCLI and Firebase led to encountering this issue... How should I proceed? ERROR in node_modules/angularfire2/app/firebase.app.module.d.ts(5,22): error TS2420: Class 'FirebaseApp' does not properly implement interface ...

Error: The variable "document" is not defined in the context of NextJS TypeScript

Here is the import trace for the requested module: ./app/StarrySky.tsx ./app/resume/page.tsx ✓ Compiled in 425ms (711 modules) ⨯ app\StarrySky.tsx (9:4) @ document ⨯ ReferenceError: document is not defined Trying to set const vw equal to the ma ...

Tips for importing a file with a dynamic path in Angular 6

I'm facing an issue where I need to import a file from a path specified in a variable's value. For example, it looks like this: let test = require(configurationUrl); Here, configurationUrl represents a path such as '../../assets/app.conf.j ...

Angular 7's Cross-Origin Resource Sharing (CORS) Configuration

Hey there! I've been struggling with getting the CORS to work. I stumbled upon this helpful post that pointed me in the right direction. Following the link provided in that post to angular.io, I implemented the solution suggested. Let me describe my ...