angular 2 remove all items from a formarray

后端 未结 15 1728
清歌不尽
清歌不尽 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条回答
  •  -上瘾入骨i
    2020-12-07 16:09

    As of Angular 8+ you can use clear() to remove all controls in the FormArray:

    const arr = new FormArray([
       new FormControl(),
       new FormControl()
    ]);
    console.log(arr.length);  // 2
    
    arr.clear();
    console.log(arr.length);  // 0
    

    For previous versions the recommended way is:

    while (arr.length) {
       arr.removeAt(0);
    }
    

    https://angular.io/api/forms/FormArray#clear

提交回复
热议问题