Reputation: 41
formGroup: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.createForm();
}
createForm() {
this.formGroup = this.formBuilder.group({
'username': ['', Validators.required],
'password': ['', Validators.required],
});
}
getError(el) {
switch (el) {
case 'user':
if (this.formGroup.get('username').hasError('required')) {
return 'login is required';
}
break;
case 'pass':
if (this.formGroup.get('password').hasError('required')) {
return 'password is required';
}
break;
default:
return '';
}
}
login angualr app
error TS2564: Property 'formGroup' has no initializer and is not definitely assigned in the constructor.
how do i fix it?
Upvotes: 1
Views: 3211
Reputation: 27303
You can use non-null assertion operator to prevent the type checker from throwing error
formGroup!: FormGroup;
Or
Move all the formGroup intialization to top level
formGroup = this.formBuilder.group({
'username': ['', Validators.required],
'password': ['', Validators.required],
});
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
}
Upvotes: 2
Reputation: 742
This is something related to the strict initialization.
formGroup!: FormGroup;
formGroup: FormGroup | undefined
Upvotes: 5