I am using Angular 9.
I am getting the following error:
No provider for FormBuilder
It has been reported in many cases, and the general solution appears to be to add the FormsModule
to the app.module.ts
file.
e.g.
JavaScript
x
5
1
import { FormsModule } from '@angular/forms';
2
3
imports: [
4
FormsModule
5
I have tried this, but am still getting the error.
JavaScript161ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[FormBuilder -> FormBuilder -> FormBuilder]:
2
3NullInjectorError: No provider for FormBuilder!
4NullInjectorError: R3InjectorError(AppModule)[FormBuilder -> FormBuilder -> FormBuilder]:
5NullInjectorError: No provider for FormBuilder!
6
Question
Am I maybe doing something that’s changed in Angular 9? What do I need to do to fix this?
My code:
login.component.ts
JavaScript
1
42
42
1
import { Component, OnInit } from '@angular/core';
2
import {Router} from '@angular/router';
3
import { FormGroup, FormBuilder } from '@angular/forms';
4
import { AuthService } from '../../services/auth.service';
5
6
@Component({
7
selector: 'app-login',
8
templateUrl: './login.component.html',
9
styleUrls: ['./login.component.css']
10
})
11
export class LoginComponent implements OnInit {
12
13
loginForm: FormGroup;
14
15
constructor(private authService: AuthService, private formBuilder: FormBuilder, private router: Router) {
16
}
17
18
ngOnInit(): void {
19
this.loginForm = this.formBuilder.group({
20
username: [''],
21
password: ['']
22
});
23
}
24
25
get f() { return this.loginForm.controls; }
26
27
login() {
28
this.authService.login(
29
{
30
username: this.f.username.value,
31
password: this.f.password.value
32
}
33
)
34
.subscribe(success => {
35
if (success) {
36
this.router.navigate(['/secret-random-number']);
37
}
38
});
39
}
40
41
}
42
app.module.ts
JavaScript
1
49
49
1
import { BrowserModule } from '@angular/platform-browser';
2
import { NgModule } from '@angular/core';
3
import { HttpClientModule } from '@angular/common/http';
4
5
import { AppRoutingModule } from './app-routing.module';
6
import { AppComponent } from './app.component';
7
import { LoginComponent } from './auth/containers/login/login.component';
8
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
9
import { FlexLayoutModule } from '@angular/flex-layout';
10
import { MatFormFieldModule } from '@angular/material/form-field';
11
import { MatInputModule } from '@angular/material/input';
12
import { MatButtonModule } from '@angular/material/button';
13
import { MatCardModule } from '@angular/material/card';
14
import { MatToolbarModule } from '@angular/material/toolbar';
15
import { FormsModule } from '@angular/forms';
16
import { ApprovalListComponent } from './approval-list/component/approval-list.component';
17
import { MatTableModule } from '@angular/material/table';
18
import { MatPaginatorModule } from '@angular/material/paginator';
19
import { MatSortModule } from '@angular/material/sort';
20
import { MatTableExporterModule } from 'mat-table-exporter';
21
22
@NgModule({
23
declarations: [
24
AppComponent,
25
LoginComponent,
26
ApprovalListComponent
27
],
28
imports: [
29
BrowserModule,
30
AppRoutingModule,
31
HttpClientModule,
32
BrowserAnimationsModule,
33
FlexLayoutModule,
34
MatFormFieldModule,
35
MatInputModule,
36
MatButtonModule,
37
MatCardModule,
38
MatToolbarModule,
39
FormsModule,
40
MatTableModule,
41
MatPaginatorModule,
42
MatSortModule,
43
MatTableExporterModule
44
],
45
providers: [LoginComponent],
46
bootstrap: [AppComponent]
47
})
48
export class AppModule { }
49
Advertisement
Answer
form builder is ReactiveFormsModule
JavaScript
1
5
1
import { ReactiveFormsModule } from '@angular/forms';
2
3
imports: [
4
ReactiveFormsModule
5
you’ll also need this module to use formGroup
and formControl
directives.