I am trying to use ngModel to do data binding, however, I get an error ngForm not found. I have already included formsmodule in app.module.ts as you can see below. The error disappears when i remove #grantAccessForm= "ngForm" but when i do this and input data in input field and submit, my page refreshes and the refreshed page has params in the url, like this: "http://localhost:8100/signup?name=a&userName=a&[email protected]&password=aa&confirm=aa"
I want to be able to do something in the onSubmit() function without the page being refreshed, can someone explain to me what #grantAccessForm= "ngForm" this means and why am i getting this error.
app-routing.module.ts
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
import { LoginPageComponent } from './login/login-page/login-page.component';
import { SignUpPageComponent } from './login/sign-up-page/sign-up-page.component';
const routes: Routes = [
{
path: 'home',
loadChildren: () => import('./home/home.module').then( m => m.HomePageModule)
},
{
path: '',
component: LoginPageComponent
},
{
path: 'login',
component: LoginPageComponent,
//()=> import('./login/login-page/login-page.component').then(m=> m.LoginPageComponent)
},
{
path: 'signup',
component: SignUpPageComponent
}
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
BrowserModule,
FormsModule,
IonicModule.forRoot(),
AppRoutingModule,
ReactiveFormsModule
],
exports:[],
providers: [
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}
sign-up-page.component.html
</ion-content>
<form #grantAccessForm= "ngForm" (ngSubmit)="onSubmit(grantAccessForm)">
<ion-grid>
<ion-row justify-content-center>
<ion-col align-self-center size-md="6" size-lg="5" size-xs="12">
<div class="centerText">
<h3>Create your account!</h3>
</div>
<div padding>
<ion-item>
<ion-input name="name" type="text" placeholder="Name" [(ngMode)]="dataModel.name"></ion-input>
</ion-item>
<ion-item>
<ion-input name="userName" type="text" placeholder="Username" [(ngModel)]="dataModel.username"></ion-input>
</ion-item>
<ion-item>
<ion-input name="email" type="email" placeholder="[email protected]" [(ngModel)]="dataModel.email"></ion-input>
</ion-item>
<ion-item>
<ion-input name="password" type="password" placeholder="Password" [(ngModel)]="dataModel.password"></ion-input>
</ion-item>
<ion-item>
<ion-input name="confirm" type="password" placeholder="Password again" [(ngModel)]="dataModel.passwordAgain"></ion-input>
</ion-item>
</div>
<div padding>
<ion-button size="large" type="submit" expand="block" >Register</ion-button>
</div>
</ion-col>
</ion-row>
</ion-grid>
</form>
</ion-content>
sign-up-page.component.ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-sign-up-page',
templateUrl: './sign-up-page.component.html',
styleUrls: ['./sign-up-page.component.scss'],
})
export class SignUpPageComponent implements OnInit {
// Import ViewChild
@ViewChild('grantAccessForm', {static: false}) grantAccessForm: NgForm;
dataModel: any = {}; // Your data model
constructor() { }
ngOnInit() {}
onSubmit(form) {
console.log(form);
}
}
How do I fix ng8003 no directive found with Exportas ngForm? Debugging the errorlink Ensure that all dependencies are properly imported and declared in our Modules. For example, if the export not found is ngForm , we will need to import FormsModule and declare it in our list of imports in *. module.
This is common with a missing import or a missing exportAs on a directive. This is the compiler equivalent of a common runtime error NG0301: Export Not Found.
i had the same issue so you need to add you singUpComponent in the app.module.ts
I faced the similar issue, in my case I was trying to open that component from a ModalController.
The solution is in app.module.ts under imports you need to add FormsModule & you need to add the component in declarations array.
The below mentioned app.module.ts should work
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import {SignUpPageComponent} from './sign-up-page.component';
import { AppRoutingModule } from './app-routing.module';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [AppComponent,SignUpPageComponent],
entryComponents: [],
imports: [
BrowserModule,
FormsModule,
IonicModule.forRoot(),
AppRoutingModule,
ReactiveFormsModule
],
exports:[],
providers: [
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With