angular - 如何清除 Angular react 形式的 FormArray 值?

标签 angular

在这里,我想让 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/

相关文章:

javascript - Angular2全局配置文件

angular - 理解复杂的 Type Script 类型声明

javascript - 在 Angular 2 App 中转换双向绑定(bind)属性中的日期

Angular 2 和 Typescript - 从单个文件导入多个接口(interface)

node.js - 如何为生产准备 nodejs?

javascript - 在 AtScript 中声明 Angular 2.0 装饰器的正确语法是什么?

angular - 重置 Angular 7 响应验证

angular - 在 Ionic 2 中隐藏状态栏

Angular 4属性无故更新

angular - TemplateRef 需要类型参数