I'm trying to build a 'edit in place' input form , to change the name of a product when the user clicks on the field. My problem is the name field in question is printed out using a ngFor loop and I don't know how many there will be, it will be different for each user. Is there a way to dynamically change the nameField and then refer to the dynamic name in the form? I guess something like: nameField1, nameField2 etc etc, but made using loop from the ngFor="let item of crudService.subscriptions.subscriptions
html:
<tr *ngFor="let item of crudService.subscriptions.subscriptions; index as i">
<td>
<div *ngIf="!showEditName">
<div>
{{ item.productName }}
</div>
<div>
<button class="btn-sc btn-blac" (click)="editName(item.productName, i)">Edit</button>
</div>
</div>
<div class="col" *ngIf="showEditName">
<div class="md-form">
<input mdbInput mdbValidate type="text" class="form-control" [formControl]="nameField" />
<label for="editName">EditName</label>
</div>
<div>
<button class="btn-sc btn-blac" (click)="editName()">save</button>
<button class="btn-sc btn-blac" (click)="editName()">cancel</button>
</div>
</div>
</td>
</tr>
component.ts:
import { FormControl } from "@angular/forms";
export class SubscriptionsComponent implements OnInit {
nameField = new FormControl("");
showEditName: boolean = false;
editName(itemName: string, subIndex: number) {
console.log(subIndex);
this.nameField.setValue(itemName);
this.showEditName = !this.showEditName;
}
}
I've seen formArray with FormGroup builder in Angular but I'm finding it hard to understand and I'm not sure if its what I need.