7 - Reactive Forms

In this section we examine Reactive forms.

1. Add ReactiveFormsModule to Auth module

libs/auth/src/lib/auth.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Route } from '@angular/router';
import { LoginComponent } from './containers/login/login.component';
import { LoginFormComponent } from './components/login-form/login-form.component';
import { HttpClientModule } from '@angular/common/http';
import { MaterialModule } from '@demo-app/material';
import { ReactiveFormsModule } from '@angular/forms';  // Added

export const authRoutes: Route[] = [
  { path: 'login', component: LoginComponent }
];
@NgModule({
  imports: [
    CommonModule,
    RouterModule,
    HttpClientModule,
    MaterialModule,
    ReactiveFormsModule   // added
  ],
  declarations: [LoginComponent, LoginFormComponent]
})
export class AuthModule {}

2. Add a Reactive FormGroup to Login Form

Note: To save injecting the formBuilder and keeping this a presentational component with no injected dependancies we can just new up a simple FormGroup. You can read more about it here https://angular.io/api/forms/FormBuilder.

3. Add HTML markup to the form

4. Add a User interface

  • Add a User interface to the data-models folder to strongly type the return value of the Auth Service.

  • Re-export the interface

5. Add types to Auth service

6. Check validation in the browser

Form validation errors

Last updated