question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reacting form is reloading the page on submit

See original GitHub issue

Bug Report

Ionic Info Run ionic info from a terminal/cmd prompt and paste the output below.

Ionic:

   ionic (Ionic CLI)          : 4.0.0-rc.9 (/home/javebratt/.npm-global/lib/node_modules/ionic)
   Ionic Framework            : @ionic/angular 4.0.0-alpha.13
   @angular-devkit/core       : 0.7.0-rc.3
   @angular-devkit/schematics : 0.7.0-rc.3
   @angular/cli               : 6.0.8
   @ionic/ng-toolkit          : 1.0.0-rc.11
   @ionic/schematics-angular  : 1.0.0-rc.11

Cordova:

   cordova (Cordova CLI) : 8.0.0
   Cordova Platforms     : none

System:

   NodeJS : v10.7.0 (/usr/bin/node)
   npm    : 6.2.0
   OS     : Linux 4.17

Describe the Bug I’m creating a reactive form, assigning: <form [formGroup]="loginForm" (ngSubmit)="loginUser(loginForm)"> but when I click on the <ion-button type="submit" expand="block"> the form doesn’t call the function loginForm(), instead it just reloads the page.

Steps to Reproduce It happened on an app I was updating, but I created a blank project to test this and it still happens, so steps to reproduce the behavior would be:

  1. Create a new blank app.
  2. Create a form in home.page.html using [formGroup] and (ngSubmit) properties.
  3. Add a button to the form: <ion-button type="submit">
  4. Initialize the form in the home.page.ts file
  5. Import and add ReactiveFormsModule to home.module.ts
  6. Run the app using ionic serve, click the submit button and see the page reload.

Related Code The HTML file:

<ion-content padding>
  <form [formGroup]="loginForm" (ngSubmit)="loginUser(loginForm)">
    <ion-item>
      <ion-label stacked>Email</ion-label>
      <ion-input formControlName="email" type="email" placeholder="Your email address">
      </ion-input>
    </ion-item>

    <ion-item>
      <ion-label stacked>Password</ion-label>
      <ion-input formControlName="password" type="password" placeholder="Your password">
      </ion-input>
    </ion-item>

    <ion-button type="submit" expand="block">
      Log In
    </ion-button>
  </form>
</ion-content>
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

@Component({
  selector: 'app-page-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  public loginForm: FormGroup;
  constructor(private formBuilder: FormBuilder) {
    this.loginForm = this.formBuilder.group({
      email: ['', Validators.compose([Validators.required, Validators.email])],
      password: [
        '',
        Validators.compose([Validators.required, Validators.minLength(6)]),
      ],
    });
  }

  loginUser(loginForm): void {
    console.log(loginForm);
  }
}

Expected Behavior When the button is clicked it should call the function and not reload the page.

Additional Context I’ve tried this using both (ngSubmit) and (submit) getting the same behavior in both.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
javebrattcommented, Aug 2, 2018

I just tested beta.1 and it’s working as expected, gonna close this 😃

1reaction
johneastcommented, Jul 28, 2018

I’m having the same issue. I can get around it by either changing

<ion-button type="submit">Submit</ion-button>

to

<button type="submit">Submit</button>

of course I lose all the styling. I can also get around it by changing the type of the ion-button from submit to a normal button.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic React form submit refreshes entire page
To prevent basic React form submit from refreshing the entire page, we call e.preventDefault. in the submit event handler.
Read more >
Prevent page refresh on form submit in React.js
Use the `preventDefault()` method on the event object to prevent a page refresh on form submit in React, e.g. `event.preventDefault()`.
Read more >
Webpack refreshes page on submit? · Discussion #6940
Hi. I just started using react-hook-form, and I'm having a problem where the page is refreshed when the form submits.
Read more >
Submit a Form Without Page Refresh Using jQuery - Code
A great way to improve the user experience of your website is to validate and submit forms without a page refresh.
Read more >
How to Refresh a Page or Component in React
The first way of refreshing a page or component is to use vanilla JavaScript to call the reload method to tell the browser...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found