2

Here, I want to make the FormArray's length zero. (it is mentioned that everything works fine with this form)

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>

here, I want to empty the nod_tags value and console.log() should return an empty array.

1

6 Answers 6

4

In the resetTags(), get the corresponding value of the FormControl that you require. Now you can call the reset() on it as shown below:

resetTags() {
   this.profileForm.controls['nod_tags'].reset();
}

From your comment, to set it to an empty array use:

resetTags() {
   let arr = <FormArray>this.profileForm.controls['nod_tags'];
   arr.clear();
}
Sign up to request clarification or add additional context in comments.

5 Comments

It worked but null value is there after resetting the nod_tags property. Here I need to make the nod_tags array empty like this [ ].
Of course Sir, here it sets 2 empty strings but I need to make the array completely empty. Thanks sir.
sorry sir, console shows 'arr.clear() is not a function', even editor mark the clear() with red underline. But why! Is there any problem with any other issues. My Angular version is 7.2.15 and CLI version is 7.3.9.
Which version of ES are you using? It worked fine for me.
Also, if that didn't work all you need to do is to clear the array. You already have the reference to it (i.e arr), now you could clear it anyway you want. If the edit didn't work for you, there are multiple other ways to clear an array in js/ts.. etc
4
resetTags() {
    const arr = this.profileForm.controls.nod_tags as FormArray;
    while (0 !== arr.length) {
      arr.removeAt(0);
}

This way worked very well. Thanks everyone.

Comments

2

The best way to do this as of Angular 8+ is with the clear() method provided with FormArray:

// Reset all tags (Angular 8+)
resetTags() {
  const nodTags = this.profileForm.get('nod_tags') as FormArray;

  if ( nodTags.length > 0 )
    nodTags.clear();
}

If you're using Angular 7 or below, then Mizanur's version will work. Here's a modified version of the resetTags() method that uses it:

// Reset all tags (Angular 7 and below)
resetTags() {
  const nodTags = this.profileForm.get('nod_tags') as FormArray;

  while ( nodTags.length > 0 ) {
      nodTags.removeAt(0);
}

Comments

2

try this,

this.form = this._formB.group({
    blocks: this._formB.array(["a","b","c"])
});

this.form.controls['blocks'].value.length = 0;

Comments

1

I think this is the way you want to do it

resetTags() {
    this.nodTags.clear();
 }

 get nodTags(): FormArray {
   return this.profileForm.get('nod_tags') as FormArray;
 }

I also made a small demo

11 Comments

Thank you sir, but It's not working for me! it shows an error in the console (ERROR TypeError: "this.nodTags.clear is not a function").
did you add get nodTags() getter that I've posted?
yes sir, I've completely followed your code. Even the editor marks the clear() with red underline.
please, double check demo. Other way to do it is with the following (<FormArray>this.profileForm.get('nod_tags')).clear(); .
when I hover over the red underlined clear() , it shows a message 'Property 'clear' does not exist on type 'FormArray'.
|
0

there is more simple way to make nod_tags Empty!

get nod_tagsArray() {
    return this.profileForm.controls.nod_tags as FormArray;
  }

    this.nod_tagsArray.controls =[];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.