I have an error to get an error on my reactive form (Angular 11).
when i want to show error message when my input email and password haven't set, but i have an error Object is possibly null
my login.component.html
<div class="row mt-5">
<div class="col-md-6 mx-auto">
<form [formGroup]="loginForm">
<div class="form-group">
<label for="email">Email</label>
<input
formControlName="email"
type="text"
class="form-control"
id="email"
required
>
<div Class="text-danger">
<div *ngIf="loginForm.get('email').hasError('required')">Email is Required !</div>
</div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input
formControlName="password"
type="password"
class="form-control"
id="password"
required
>
<div Class="text-danger" >
<div *ngIf="loginForm.get('password').hasError('required')">Password is Required !</div>
<div *ngIf="loginForm.get('password').hasError('minlength')">Password was to be 8 Caracters !</div>
</div>
</div>
{{loginForm.value | json}}
<button [disabled]="loginForm.invalid" class="btn btn-primary btn-block">Sign In</button>
</form>
</div>
</div>
my login.component.ts
loginForm: FormGroup ;
constructor() { }
ngOnInit(): void {
this.loginForm = new FormGroup({
email: new FormControl(null, [Validators.required]),
password: new FormControl(null, [Validators.required, Validators.minLength(8)])
});
}
get email() { return this.loginForm.get('email'); }
get password() { return this.loginForm.get('password'); }
Can you tell me why i haven't to get the error message and i have compilation error called Object is possibly null. In app.module.ts file , i had the formsModule and ReactiveModule in import section.