I am displaying a FormArray with an *ngFor. What i am trying to do is when i click on an item in the ngFor, to populate the with that items 'task' property.
Additionally, when i type/update the contents of input, the original form is updated/patched.
HTML:
<form [formGroup]="myForm">
<div [id]="i"
class="example-box"
*ngFor="let item of myForm.get('items').controls; let i=index;"
(click)="updateInput(item)">
{{item.value.task}} to be
</div>
<br>
<br>
<input formControlName="xxx" />
<br>
<br>
{{ myForm.value | json}}
</form>
Component.ts:
export class CdkDragDropSortingExample {
nominatedArray = [];
myForm: FormGroup;
constructor(private fb: FormBuilder) {
this.myForm = this.fb.group({
title: ['title'],
items: fb.array([
fb.group({
completed: fb.control(false),
task: fb.control('Set Alarm'),
position: fb.control(0)
}),
fb.group({
completed: fb.control(false),
task: fb.control('Brush teeth'),
position: fb.control(1)
}),
fb.group({
completed: fb.control(false),
task: fb.control('Shower'),
position: fb.control(2)
}),
fb.group({
completed: fb.control(false),
task: fb.control('Get ready'),
position: fb.control(3)
}),
fb.group({
completed: fb.control(false),
task: fb.control('Have breakfast'),
position: fb.control(4)
})
])
})
}
updateInput(item) {
console.log(item);
}
}
Stackblitz: https://stackblitz.com/edit/angular-asevei-t5pm9u