Angular - Reactive form - FormArray [closed]












-3















I am trying to implement add multiple lines using Angular Reactive - FormArray.



In the example I have 2 text fields. After entering the data and click the add button I want to add it to the lines.



After adding I am not able to clear the data entry 2 text fields.
And the last added element in the array is still linked to data entry.



Is FormArray suitable in this case, or should I use a simple approach please



Here is my code.



https://stackblitz.com/edit/jay-angular-reactive-array-mwhmcn










share|improve this question













closed as off-topic by R. Richards, Jörg W Mittag, user6655984, HDJEMAI, gnat Nov 15 '18 at 5:48


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – R. Richards, Jörg W Mittag, Community, HDJEMAI

If this question can be reworded to fit the rules in the help center, please edit the question.





















    -3















    I am trying to implement add multiple lines using Angular Reactive - FormArray.



    In the example I have 2 text fields. After entering the data and click the add button I want to add it to the lines.



    After adding I am not able to clear the data entry 2 text fields.
    And the last added element in the array is still linked to data entry.



    Is FormArray suitable in this case, or should I use a simple approach please



    Here is my code.



    https://stackblitz.com/edit/jay-angular-reactive-array-mwhmcn










    share|improve this question













    closed as off-topic by R. Richards, Jörg W Mittag, user6655984, HDJEMAI, gnat Nov 15 '18 at 5:48


    This question appears to be off-topic. The users who voted to close gave this specific reason:


    • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – R. Richards, Jörg W Mittag, Community, HDJEMAI

    If this question can be reworded to fit the rules in the help center, please edit the question.



















      -3












      -3








      -3








      I am trying to implement add multiple lines using Angular Reactive - FormArray.



      In the example I have 2 text fields. After entering the data and click the add button I want to add it to the lines.



      After adding I am not able to clear the data entry 2 text fields.
      And the last added element in the array is still linked to data entry.



      Is FormArray suitable in this case, or should I use a simple approach please



      Here is my code.



      https://stackblitz.com/edit/jay-angular-reactive-array-mwhmcn










      share|improve this question














      I am trying to implement add multiple lines using Angular Reactive - FormArray.



      In the example I have 2 text fields. After entering the data and click the add button I want to add it to the lines.



      After adding I am not able to clear the data entry 2 text fields.
      And the last added element in the array is still linked to data entry.



      Is FormArray suitable in this case, or should I use a simple approach please



      Here is my code.



      https://stackblitz.com/edit/jay-angular-reactive-array-mwhmcn







      angular angular-reactive-forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 23:11









      JayJay

      4,45752655




      4,45752655




      closed as off-topic by R. Richards, Jörg W Mittag, user6655984, HDJEMAI, gnat Nov 15 '18 at 5:48


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – R. Richards, Jörg W Mittag, Community, HDJEMAI

      If this question can be reworded to fit the rules in the help center, please edit the question.







      closed as off-topic by R. Richards, Jörg W Mittag, user6655984, HDJEMAI, gnat Nov 15 '18 at 5:48


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – R. Richards, Jörg W Mittag, Community, HDJEMAI

      If this question can be reworded to fit the rules in the help center, please edit the question.
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I did not get what are trying to do with FormArray, thus I suggest a simpler way to achieve what you want.



          Change your TS to:



          export class AppComponent  {

          constructor(private fb: FormBuilder) { }
          public simpleArray = ;
          form: FormGroup;

          ngOnInit() {

          this.form = this.fb.group({
          country1: ['', Validators.required],
          service1: ['', Validators.required]
          });

          }

          add() {
          this.simpleArray.push(this.form.value);
          //this.form.reset();
          this.form.get('country1').setValue('');
          this.form.get('service1').setValue('');
          }


          deleteService(i) {
          this.simpleArray.splice(i,1)
          }
          }


          And your HTML should be :



          <form [formGroup]="form">
          <h3>Angular Reactive form - FormArray Demo </h3>
          Form : {{ simpleArray | json}}
          <br/><br/>
          <div class="form-group">
          <label for="country">Country</label>
          <input required type="text" id="country" class="form-control" formControlName="country1" />
          </div>
          <div class="form-group">
          <label for="service">Serivce</label>
          <input required type="text" id="service" class="form-control" formControlName="service1" />
          </div>
          <br />
          <button type='button' class="btn btn-primary" (click)='add()'>Add Services</button>
          <br />
          <div >
          <div class='row' *ngFor="let line of simpleArray let i=index">
          <input class='form-control col-md-2' [value]="line.country1" />
          <input class='form-control col-md-2' [value]="line.service1"/>
          <button class='btn btn-primary col-md-1' (click)="deleteService(i)">Delete</button>
          </div>
          </div>
          <br />
          </form>


          A working Stackblitz






          share|improve this answer
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I did not get what are trying to do with FormArray, thus I suggest a simpler way to achieve what you want.



            Change your TS to:



            export class AppComponent  {

            constructor(private fb: FormBuilder) { }
            public simpleArray = ;
            form: FormGroup;

            ngOnInit() {

            this.form = this.fb.group({
            country1: ['', Validators.required],
            service1: ['', Validators.required]
            });

            }

            add() {
            this.simpleArray.push(this.form.value);
            //this.form.reset();
            this.form.get('country1').setValue('');
            this.form.get('service1').setValue('');
            }


            deleteService(i) {
            this.simpleArray.splice(i,1)
            }
            }


            And your HTML should be :



            <form [formGroup]="form">
            <h3>Angular Reactive form - FormArray Demo </h3>
            Form : {{ simpleArray | json}}
            <br/><br/>
            <div class="form-group">
            <label for="country">Country</label>
            <input required type="text" id="country" class="form-control" formControlName="country1" />
            </div>
            <div class="form-group">
            <label for="service">Serivce</label>
            <input required type="text" id="service" class="form-control" formControlName="service1" />
            </div>
            <br />
            <button type='button' class="btn btn-primary" (click)='add()'>Add Services</button>
            <br />
            <div >
            <div class='row' *ngFor="let line of simpleArray let i=index">
            <input class='form-control col-md-2' [value]="line.country1" />
            <input class='form-control col-md-2' [value]="line.service1"/>
            <button class='btn btn-primary col-md-1' (click)="deleteService(i)">Delete</button>
            </div>
            </div>
            <br />
            </form>


            A working Stackblitz






            share|improve this answer






























              1














              I did not get what are trying to do with FormArray, thus I suggest a simpler way to achieve what you want.



              Change your TS to:



              export class AppComponent  {

              constructor(private fb: FormBuilder) { }
              public simpleArray = ;
              form: FormGroup;

              ngOnInit() {

              this.form = this.fb.group({
              country1: ['', Validators.required],
              service1: ['', Validators.required]
              });

              }

              add() {
              this.simpleArray.push(this.form.value);
              //this.form.reset();
              this.form.get('country1').setValue('');
              this.form.get('service1').setValue('');
              }


              deleteService(i) {
              this.simpleArray.splice(i,1)
              }
              }


              And your HTML should be :



              <form [formGroup]="form">
              <h3>Angular Reactive form - FormArray Demo </h3>
              Form : {{ simpleArray | json}}
              <br/><br/>
              <div class="form-group">
              <label for="country">Country</label>
              <input required type="text" id="country" class="form-control" formControlName="country1" />
              </div>
              <div class="form-group">
              <label for="service">Serivce</label>
              <input required type="text" id="service" class="form-control" formControlName="service1" />
              </div>
              <br />
              <button type='button' class="btn btn-primary" (click)='add()'>Add Services</button>
              <br />
              <div >
              <div class='row' *ngFor="let line of simpleArray let i=index">
              <input class='form-control col-md-2' [value]="line.country1" />
              <input class='form-control col-md-2' [value]="line.service1"/>
              <button class='btn btn-primary col-md-1' (click)="deleteService(i)">Delete</button>
              </div>
              </div>
              <br />
              </form>


              A working Stackblitz






              share|improve this answer




























                1












                1








                1







                I did not get what are trying to do with FormArray, thus I suggest a simpler way to achieve what you want.



                Change your TS to:



                export class AppComponent  {

                constructor(private fb: FormBuilder) { }
                public simpleArray = ;
                form: FormGroup;

                ngOnInit() {

                this.form = this.fb.group({
                country1: ['', Validators.required],
                service1: ['', Validators.required]
                });

                }

                add() {
                this.simpleArray.push(this.form.value);
                //this.form.reset();
                this.form.get('country1').setValue('');
                this.form.get('service1').setValue('');
                }


                deleteService(i) {
                this.simpleArray.splice(i,1)
                }
                }


                And your HTML should be :



                <form [formGroup]="form">
                <h3>Angular Reactive form - FormArray Demo </h3>
                Form : {{ simpleArray | json}}
                <br/><br/>
                <div class="form-group">
                <label for="country">Country</label>
                <input required type="text" id="country" class="form-control" formControlName="country1" />
                </div>
                <div class="form-group">
                <label for="service">Serivce</label>
                <input required type="text" id="service" class="form-control" formControlName="service1" />
                </div>
                <br />
                <button type='button' class="btn btn-primary" (click)='add()'>Add Services</button>
                <br />
                <div >
                <div class='row' *ngFor="let line of simpleArray let i=index">
                <input class='form-control col-md-2' [value]="line.country1" />
                <input class='form-control col-md-2' [value]="line.service1"/>
                <button class='btn btn-primary col-md-1' (click)="deleteService(i)">Delete</button>
                </div>
                </div>
                <br />
                </form>


                A working Stackblitz






                share|improve this answer















                I did not get what are trying to do with FormArray, thus I suggest a simpler way to achieve what you want.



                Change your TS to:



                export class AppComponent  {

                constructor(private fb: FormBuilder) { }
                public simpleArray = ;
                form: FormGroup;

                ngOnInit() {

                this.form = this.fb.group({
                country1: ['', Validators.required],
                service1: ['', Validators.required]
                });

                }

                add() {
                this.simpleArray.push(this.form.value);
                //this.form.reset();
                this.form.get('country1').setValue('');
                this.form.get('service1').setValue('');
                }


                deleteService(i) {
                this.simpleArray.splice(i,1)
                }
                }


                And your HTML should be :



                <form [formGroup]="form">
                <h3>Angular Reactive form - FormArray Demo </h3>
                Form : {{ simpleArray | json}}
                <br/><br/>
                <div class="form-group">
                <label for="country">Country</label>
                <input required type="text" id="country" class="form-control" formControlName="country1" />
                </div>
                <div class="form-group">
                <label for="service">Serivce</label>
                <input required type="text" id="service" class="form-control" formControlName="service1" />
                </div>
                <br />
                <button type='button' class="btn btn-primary" (click)='add()'>Add Services</button>
                <br />
                <div >
                <div class='row' *ngFor="let line of simpleArray let i=index">
                <input class='form-control col-md-2' [value]="line.country1" />
                <input class='form-control col-md-2' [value]="line.service1"/>
                <button class='btn btn-primary col-md-1' (click)="deleteService(i)">Delete</button>
                </div>
                </div>
                <br />
                </form>


                A working Stackblitz







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 0:26

























                answered Nov 15 '18 at 0:19









                selem mnselem mn

                5,09541939




                5,09541939

















                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python