'Angular - cannot use patchvalue for form editing

I am working on a form where I need to be able to edit the form entry once submitted. As you will see in the code, the form works well, each entry appears well as a line below the form. However, I cannot manage to modify each form entry. If I split the task, it would be to send the data to the form, edit it there and then submit it again.

I think I have to use patchvalue, but I cannot manage to make it work. Any idea? This is my first Angular project and I am bit lost.

Thanks for the help!

Here is the html

<div class="formulario" >
<h1>Formulario</h1>

<form [formGroup]="person" (ngSubmit)="onsubmit()">
 
  <mat-form-field appearance="fill">
    <label>Introduce tu nombre</label>
    <input matInput #input id="nombre" type="text" placeholder="Nombre (min 3 car)" formControlName="nombre">      
  </mat-form-field>

  <br/>
  <mat-form-field appearance="fill">
    <label>Introduce tus apellidos</label>
    <input matInput #input id="apellidos" type="text" placeholder="Apellidos (min 3 car)" formControlName="apellidos"/>
  </mat-form-field>
  <br/>
<button type = "submit" > Enviar tus datos</button>

    </form>
  </div>



<table>
    <thead>
        <th>Posición</th>
        <th>Nombre</th>
        <th>Apellidos</th>
        <th> Modificar</th>
    </thead>
    <tbody>
        <tr *ngFor="let persona of personas; let i = index">
            <td>{{i}}</td>
            <td>{{persona.nombre}}</td>
            <td>{{persona.apellidos}}</td>
            <td> <button mat-flat-button (click)="edititem(i)" >Modificar</button></td>
      <td>
        </tr>
    </tbody>
</table>

Here is my ts :

    import { Component, OnInit, ViewChild } from '@angular/core';
import { Persona } from 'src/app/components/formulario/persona';
import { FormControl, FormBuilder, FormGroup, Validators, FormGroupDirective, ReactiveFormsModule } from '@angular/forms';
import {MatListModule} from '@angular/material/list';
import { SelectionModel } from '@angular/cdk/collections';


@Component({
  selector: 'app-formulario',
  templateUrl: './formulario.component.html',
  styleUrls: ['./formulario.component.css']
})

export class FormularioComponent implements OnInit {
    person: FormGroup = new FormGroup({});
    personas : Persona[] = [];

    constructor(){
        this.person = new FormGroup({
            nombre : new FormControl("", [Validators.required, Validators.minLength(3)]),
            apellidos : new FormControl("", [Validators.required,  Validators.minLength(3)]),
        });
    }

    ngOnInit() : void{
        }

    onsubmit() {
        let persona = new Persona();

        persona.nombre = this.person.value.nombre;
        persona.apellidos = this.person.value.apellidos;

        this.personas.push(persona);
        this.person.reset();
    }

    edititem(i : number) : void {
            this.person.patchValue({i})
            this.person.get('nombre')?.setValue(this.person.value.nombre)
            this.person.get('apellidos')?.setValue(this.personas[i])

    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source