1

Is it possible that i will be able to add rows based on certain condition in Angular reactive forms? If yes, please help me do it. I've already started it in my stackblitz. Please check this link https://stackblitz.com/edit/dynamically-add-rows

this.addForm.get("items").valueChanges.subscribe(val => {
      console.log(val);
      if (val === true) {
        this.addForm.get("items_value").setValue("yes");

        // How to add row here?
        this.addForm.addControl(rows, 'rows')
      }
      if (val === false) {
        this.addForm.get("items_value").setValue("no");

        // How to remove row here?
        this.addForm.removeControl(rows, 'rows')
      }
    });
4
  • i have a question . u want to add or remove many rows ? or just and or remove one row? Commented Sep 9, 2018 at 6:41
  • @Soroush_Neshat. At first, when the condition is met. It should only show one row. But i can also add another row because i will add a button to it. Commented Sep 9, 2018 at 6:46
  • i am not sure if i got your problem correctly . i will add an initial answer to your question and see if it solves your issue Commented Sep 9, 2018 at 6:55
  • @Soroush_Neshat. Ok. no problem Commented Sep 9, 2018 at 6:56

1 Answer 1

1

here is the edited stackblitz :https://stackblitz.com/edit/dynamically-add-rows-gk4veg?file=app%2Fapp.component.html

Sign up to request clarification or add additional context in comments.

18 Comments

I'm sorry i just need many rows. Is it possible to something like this? this.addForm.addControl('rows',new fb:array[]())?
@Joseph ok . give me a second to update the question
@Joseph see the link i added
Can you put in my stackblitz since i want to have based it on the condition. If val is yes, then add that row, if val is no, then remove one row or several rows
@Joseph if val is no , then how should i decide to remove one row , or many rows ?
|

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.