Reacting form is reloading the page on submit
See original GitHub issueBug 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:
- Create a new blank app.
- Create a form in
home.page.html
using[formGroup]
and(ngSubmit)
properties. - Add a button to the form:
<ion-button type="submit">
- Initialize the form in the
home.page.ts
file - Import and add
ReactiveFormsModule
tohome.module.ts
- 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:
- Created 5 years ago
- Reactions:3
- Comments:8 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I just tested beta.1 and it’s working as expected, gonna close this 😃
I’m having the same issue. I can get around it by either changing
to
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.