2

Greeting. I've tried multiple ways, but none of them work. I'm on this, right now. I need to initiate a formArray of emails

email: [testestest]

But I got:

email: [testestest]

But my email return n click. Check my ts

And my html:

1
  • Can you create a stackblitz to replicate the issue? Commented Feb 16, 2020 at 14:02

1 Answer 1

2

Alex, you has an Form Array of FormControls, not an Form Array of formGroups. So you .html must be like

<form *ngIf="addForm" [formGroup]="addForm">
    <div class="form-control-row" formArrayName="email"
        *ngFor="let item of addForm.get('email').controls; let i = index;">
        <div class="input-box">
            <input type="text" placeholder="E - mail" [formControlName]="i">
            <img src="../../assets/Delete.svg" alt="x-icon">
      </div>
        </div>
</form>

See that you has no [fomGroupName]="i", and you use [formControlName]="i"

Other way is

<form *ngIf="addForm" [formGroup]="addForm">
    <div class="form-control-row" formArrayName="email"
        *ngFor="let item of addForm.get('email').controls; let i = index;">
        <div class="input-box">
            <input type="text" placeholder="E - mail" [formControl]="item">
            <img src="../../assets/Delete.svg" alt="x-icon">
      </div>
        </div>
</form>

See that in this case we use [formControl]="item" (item is the variable you use in the *ngFor)

Well, how you create the formArray?

If you has an array of emails you can do

    email:  this.formBuilder.array(
             this.email.map(x=>this.formBuilder.control(x))
          )

That's you create an array of FormControls using map (transform each element of the array in a FormControl who value the element of the array.

NOTE: In production you need create a getter for the array

get emails()
{
    return this.form.get('email') as FormArray;
}

And iterate over

    *ngFor="let item of emails.controls; let i = index;">

The use of [formGroupName]="i" and formControl it's used with a FormArray of FormGroups. e.g. you can has

    email:  this.formBuilder.array(
             this.email.map(x=>this.formBuilder.group({email:x}))
          )

See that in this case transform each element of the array in a formGroup that has a FormControl "called" email

You can see all this in this stackblitz. I hope the example help you understand the diference

If we want to add an element to the formArray we can make a function

  addEmail(email:any)
  {
    const array=this.addForm.get('email') as FormArray 
    array.push(this.formBuilder.control(email)) //if is a FormArray of FormControl

    const array2=this.addForm.get('email2') as FormArray
    array2.push(this.formBuilder.group({email:email})) //if is a FormArray of FormGroup
  }

NOTE: At first, if we want to create a empty formArray we need use

    email:  this.formBuilder.array([])

When we want delete an element of the array, we need pass the "index" of the array, so our function delete is

delete(index:number)
  {
    const array=this.addForm.get('email') as FormArray
    array.removeAt(index)

  }

and our .html is

  <div class="input-box">
        <input type="text" placeholder="E - mail" [formControl]="item">
        <img src="../../assets/Delete.svg" alt="x-icon" (click)="delete(i)">
  </div>
Sign up to request clarification or add additional context in comments.

11 Comments

When i set email: this.formBuilder.array( this.email.map(x=>this.formBuilder.group({email:x})) ) in onInit form i got error Property 'map' does not exist on type 'FormArray'.ts(2339)
How to iterate without already created Array email: [....] ? @eliseo
@alexic93939, I update the answer (and the stackblitz) to create a function to Add an emal. About the error, I don't know where you put the code. take account that, when you create the formArray you need create as array -it's can be empty, but need to be an arrat-
I have dynamic field and I can add 3or 4 new fields..?
You save my day! @Elise Thank you! Can you please just help me to add remove single fields? Thank you!
|

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.