Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular submit form inside form?

I wanna send a form inside a form, but when I submit the form-inside-the-form, all the forms are submitted now. Is it even possible to do this?

<form name="add_foobar_form" novalidate ng-submit="addFoobar(foobar)">

    <form name="send_contact_form" novalidate ng-submit="sendContact(hello)">
        <input type="text" ng-model="hello.bye">
        <input type="submit" value="sendmall">
    </form>

    <input type="text" ng-model="foobar.go">
    <input type="submit" value="sendall">

</form>
like image 348
user1867254 Avatar asked Aug 25 '15 17:08

user1867254


People also ask

Can I have a form inside a form in Angular?

What is a Nested Form? In simple words, nested forms are forms within a form. Using nested forms, we can create an array of objects within a single field and we can have an array of these fields. Hence, the nested form helps us manage large form groups and divides it into small groups.

Can I use a form inside a form?

You can use plenty of SUBMIT buttons inside a single form, but you can't manage a nested form appropriately.

Can we use nested form in Angular?

These are the 3 steps required to create Nested Reactive Form, Step 1: create a new application in angular using the command. “ng new new-app.” Step 2: Import the forms module and reactive forms module in the app.

Can I have a form inside a form in HTML?

Every form must be enclosed within a FORM element. There can be several forms in a single document, but the FORM element can't be nested.


1 Answers

Can you have nested forms? Simply no, Nested form won't work.

Docs Says

In Angular, forms can be nested. This means that the outer form is valid when all of the child forms are valid as well. However, browsers do not allow nesting of elements, so Angular provides the ngForm directive which behaves identically to but can be nested. This allows you to have nested forms, which is very useful when using Angular validation directives in forms that are dynamically generated using the ngRepeat directive

But unfortunately you could not use the ng-submit form, that will call the parent ng-submit method on click of any inner form.

Example Plunkr

Workaround for this would be don't use ng-submit directive for the inner nested from. You should use ng-click on button and change type of button to button from submit.

Markup

<form name="add_foobar_form" novalidate ng-submit="addFoobar('foobar')">
    <ng-form name="send_contact_form" novalidate>
      <input type="text" ng-model="hello.bye">
      <input type="button" value="sendmall" ng-click="sendContact('hello')">
    </ng-form>

    <input type="text" ng-model="foobar.go">
    <input type="submit" value="sendall">
</form>

Workaround Plunkr

like image 161
Pankaj Parkar Avatar answered Oct 18 '22 23:10

Pankaj Parkar