在这里,我想让 FormArray 的长度为零。 (有人提到在这种形式下一切正常)
profileForm: FormGroup;
ngOnInit() {
this.profileForm = new FormGroup({
nod_title: new FormControl(),
nod_subtitle: new FormControl(null),
nod_name: new FormControl(null),
nod_occupation: new FormControl(null),
nod_description: new FormControl(null),
nod_tags: new FormArray([new FormControl('hello'), new FormControl('world')]),
});
}
resetTags() {
if (this.profileForm.value.nod_tags.length) { // here I tried
this.profileForm.value.nod_tags.clear();
console.log(this.profileForm.value.nod_tags)
}
<button type="button" (click)="resetTags()"> Reset taggs </button>
在这里,我想清空 nod_tags 值,console.log() 应该返回一个空数组。
最佳答案
在resetTags()
中,获取您需要的FormControl对应的值。现在您可以调用 reset()如下所示:
resetTags() {
this.profileForm.controls['nod_tags'].reset();
}
根据您的评论,将其设置为空数组使用:
resetTags() {
let arr = <FormArray>this.profileForm.controls['nod_tags'];
arr.clear();
}
关于angular - 如何清除 Angular react 形式的 FormArray 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56620173/