I want to add input dynamically and to specify formControlName and placeholder value for each added input
when i click on "plus" i want to get this result :
I managed to add an input each time the plus button is clicked but i couldn't specify the placeholder and formControlName value
This is my ts code :
addLink() {
//when the plus button is clicked
const placeholdervalue = this.addForm.get("placeholdervalue").value;
this.items = this.addForm.get("items") as FormArray;
this.items.push(this.createItem(placeholdervalue));
console.log(this.addForm.get("items"));
}
createItem(placeholdervalue: string): FormGroup {
let a = { [placeholdervalue]: "" };
return this.formBuilder.group(a);
}
ngOnInit() {
this.addForm = this.formBuilder.group({
items: this.formBuilder.array([]),
placeholdervalue: ""
});
}
}
this is my view :
<div class="row">
<div
class="col-md-3"
formArrayName="items"
*ngFor="
let item of addForm.get('items').controls;
let i = index
"
>
<div [formGroupName]="i">
<mat-form-field class="example-full-width">
<input
matInput
formControlName="" // i want to retrieve it from item
placeholder=""
/>
</mat-form-field>
</div>
</div>
</div>
div
within for loop haveformControlName
as an input property? and placeholders are not part of reactive form controls(as far I know). - Ashish Ranjan