angular 2 remove all items from a formarray

后端 未结 15 1729
清歌不尽
清歌不尽 2020-12-07 15:30

I have a form array inside a formbuilder and i am dynamically changing forms, i.e. on click load data from application 1 etc.

The issue i am having is that all the

15条回答
  •  独厮守ぢ
    2020-12-07 16:11

    To keep the code clean I have created the following extension method for anyone using Angular 7 and below. This can also be used to extend any other functionality of Reactive Forms.

    import { FormArray } from '@angular/forms';
    
    declare module '@angular/forms/src/model' {
      interface FormArray {
        clearArray: () => FormArray;
      }
    }
    
    FormArray.prototype.clearArray = function () {
      const _self = this as FormArray;
      _self.controls = [];
      _self.setValue([]);
      _self.updateValueAndValidity();
      return _self;
    }
    
    

提交回复
热议问题