Angular 角度|如何改变形式排列的顺序?
我正在我的Angular应用程序中使用FormArray构建一个表格:Angular 角度|如何改变形式排列的顺序?,angular,angular-forms,Angular,Angular Forms,我正在我的Angular应用程序中使用FormArray构建一个表格: public form = new FormGroup({ experiences: new FormArray([]) }); 用户可以向阵列添加体验: addExperience(lotsOfVars) { const formGroup = new FormGroup({ /* creating a formgroup*/}) (<FormArray> this.form.get('ex
public form = new FormGroup({
experiences: new FormArray([])
});
用户可以向阵列添加体验:
addExperience(lotsOfVars) {
const formGroup = new FormGroup({ /* creating a formgroup*/})
(<FormArray> this.form.get('experiences')).push(formGroup);
}
addExperience(lotsOfVars){
const formGroup=new formGroup({/*创建一个formGroup*/})
(this.form.get('experiences')).push(formGroup);
}
新的要求是允许用户更改先前输入体验的顺序:
问题:这是如何最好地实现的
预期结果(类似于):
moveUp(i:number){
(this.form.controls['experiences')).at(i.moveUp()中
}
您可以交换控件
// clone object (otherwise only pointer/reference is saved).
const temp = Object.assign({}, (<FormArray> this.form.controls['experiences']).at(i - 1).value);
(<FormArray> this.form.controls['experiences']).at(i - 1).setValue((<FormArray> this.form.controls['experiences']).at(i).value);
(<FormArray> this.form.controls['experiences']).at(i).setValue(temp);
//克隆对象(否则只保存指针/引用)。
const temp=Object.assign({},(this.form.controls['experiences']).at(i-1.value);
(this.form.controls['experiences']).at(i-1).setValue((this.form.controls['experiences']).at(i.value);
(此.form.controls['experiences'])在(i)设置值(temp);
要获得更详细的答案,您可以。您只需从一个索引中删除组,然后在另一个索引中插入:
let group = (<FormArray>this.form.get('address')).at(index);
(<FormArray>this.form.get('address')).removeAt(index);
(<FormArray>this.form.get('address')).insert(index,group);
let group=(this.form.get('address')).at(index);
(this.form.get('address')).removeAt(index);
(this.form.get('address')).insert(索引,组);
这是否回答了您的问题?
let group = (<FormArray>this.form.get('address')).at(index);
(<FormArray>this.form.get('address')).removeAt(index);
(<FormArray>this.form.get('address')).insert(index,group);