1

I have a basic angular reactive form inside a modal bootstrap popup which is simply not submitting. ie the angular ngSubmit event is not getting triggered! Please take a look at this stackblitz where I have simulated it:

https://stackblitz.com/edit/stackblitz-starters-xmhsxaez

Just fill in some data and try to submit the form. I have put an alert dialog inside the onSubmit() method to check if it gets called at all on form submit. Nope, doesn't work.

Currently it is giving two console errors, which is strange too, because I have bound the reactive form properties with respective input controls using formControlName.

Cannot find control with name: 'name'
Cannot find control with name: 'city'

Can you tell me what is wrong with my stackblitz attempt. What exactly did I miss?

Thanks,

2 Answers 2

1

You need to initialize the form on the ngOnInit hook, then it will start working.

ngOnInit(): void {
  this.setFormState();
}

Then you can change the button to submit the form to type="submit".

      <button type="submit" data-bs-dismiss="modal" class="btn btn-primary">
        Save Changes
      </button>

Stackblitz Demo

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

1 Comment

that did the trick! @NarenMurali . Silly me I overlooked the button type="submit" part.
0

Move this.setFormState(); from the closeModal() method to the openModal() method.

There are cleaner ways of doing this like having the modal being its component so it can have its own lifecycle and Output the value of the form back to your parent component when its closed.

1 Comment

Yes, I agree there are ways to clean up the code and make it more 'Angular'-esque. Like creating a separate component for the modal @EdwardNewsome. Even rendering of the modal can be improved by using renderer2. But the biggest thing I overlooked was setting button type="submit" instead of type="button" as NarenMurali pointed out. Silly me! How did I overlook that one!

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.