1

For any form in Angular 2.0.1, whenever I press return or click on the submit button, the ngSubmit output is fired twice. I'm already including the FormsModule in the application module:

@NgModule({
  imports: [
    BrowserModule,
    HttpModule,
    FormsModule,
    routing
  ],
  declarations: [
    // ...
  ],
  providers: [
    // ...
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

I've tried to follow some answers (such as this one), but disableDeprecatedForms and provideForms have been removed since RC6. I've also tried to import the DeprecatedFormsModule, but it seems to have been removed as well.

What should I do to prevent the ngSubmit to be fired twice?

UPDATE:

I didn't think the template would matter in this case since it's a known issue and it happens for any form in the application, but here follow one of the forms as an example (stripped out of unnecessary classes and tags):

<form (ngSubmit)="login(user)">
  <input [(ngModel)]="user.username" name="username" type="email" id="username" required>
  <label for="username">Email</label>

  <input [(ngModel)]="user.password" name="password" type="password" id="password" required>
  <label for="password">Password</label>

  <button>
    Submit
  </button>
</form>
mrodrigues
  • 1,052
  • 10
  • 14
  • Show form code in HTML including button. – micronyks Oct 19 '16 at 12:48
  • may be you are submitting your form twice firstly at the form tag and secondly on the button named submit check it once or either post you code – Pardeep Jain Oct 19 '16 at 12:48
  • Thanks for your comments, I just included the template for one of the forms. – mrodrigues Oct 19 '16 at 12:56
  • your form seems right error is somewhere else, could you please recreate your problem on some plunker ? – Pardeep Jain Oct 19 '16 at 12:56
  • Thanks, now that I've wrote the Plunker I realized it was something else. What's actually firing twice is not the `ngSubmit` as I had previously thought, but the HTTP request (I'm feeling silly I hadn't test that before). – mrodrigues Oct 19 '16 at 13:27

1 Answers1

0

After some some comments I tried to reproduce the error in a Plunker only to realize that the problem was something else entirely. What was being fired twice was not the ngSubmit, but the HTTP request.

I was subscribing twice to the same http.post(args), what happens is that I need to call .share() so it won't be executed for every subscribe (https://stackoverflow.com/a/37241863/2908285).

Community
  • 1
  • 1
mrodrigues
  • 1,052
  • 10
  • 14