问题
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
回答1:
you can declare a variable
control:FormControl
And use in your input formControl
<input [formControl]="control" />
Simply on click
(click)="control=item.get('task')
But I think you want to "edit in place". For this you need two variables and as usually I go to make a getter to the formArray
itemSelected:number=-1;
dropping:boolean=false
get items()
{
return (this.myForm.get('items') as FormArray)
}
Our .html is like
<form [formGroup]="myForm">
<div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
<div [id]="i" class="example-box"
[cdkDragDisabled]="itemSelected==i"
(cdkDragDropped)="dropping=false"
(cdkDragMoved)="dropping=true;itemSelected=-1"
*ngFor="let item of items.controls; let i=index;" cdkDrag>
<span *ngIf="itemSelected!=i" style="cursor:text"
(click)="!dropping && focus(i)" >
{{item.value.task}} to be
</span>
<input #input *ngIf="itemSelected==i" [formControl]="item.get('task')"
(blur)="itemSelected=-1">
</div>
</div>
</form>
NOTE: to use the property cdkDragDisable you need updated your references, in "@angular/cdk": "7.0.3"
you has not this property, so you need update to Angular 9 too
See how, if "i=selectedIndex" it's showed the "input" and the cdkDrag is disabled. We need mannage when we click and when we want to drag. for this, I use the variable dropping that is true when you move and false when dropped, moreover, we make nothing if is dropping is true, the (click)="!dropping && focus(i)"
Well, the function focus put the variable itemSelected to the value of the row and make the focus. The focus need make in a setTimeout to give a change to Angular to show the input
focus(i:number)
{
this.itemSelected=i
setTimeout(()=>{
this.input.nativeElement.focus()
})
}
Finally, the drop function, need take account that the function moveItemInArray is thinking to work with arrays, not with formArrays, so
drop(event: any) {
const array=this.items.value //get the value of the formArray
moveItemInArray(array, event.previousIndex, event.currentIndex); //move the value
array.forEach((x:any,i:number)=>x.position=i) //recalculate the position
this.items.setValue(array) //make a setValue
}
You can see in this stackblitz
来源:https://stackoverflow.com/questions/62304645/display-and-update-formgroup-inside-formarray