Answer the question
In order to leave comments, you need to log in
What is the error when creating a variable of type FormGroup?
Hello everyone, I have this code:
import { Component, OnInit } from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
@Component({
selector: 'app-login-page',
templateUrl: './login-page.component.html',
styleUrls: ['./login-page.component.scss']
})
export class LoginPageComponent implements OnInit {
loginForm: FormGroup;
constructor(
) { }
ngOnInit(): void {
this.loginForm = new FormGroup({
email: new FormControl('', [Validators.required , Validators.email]),
password: new FormControl('', [Validators.required , Validators.minLength(4)]),
});
}
onLogin(): void {
}
}
Answer the question
In order to leave comments, you need to log in
Need to move form initialization code to constructor()
constructor() {
this.loginForm = new FormGroup({
email: new FormControl('', [Validators.required , Validators.email]),
password: new FormControl('', [Validators.required , Validators.minLength(4)]),
});
}
Try like this
loginForm: FormGroup;
constructor(
) {
this.loginForm = 'some value'; // Некторое значение
}
ngOnInit(): void {
this.loginForm = new FormGroup({
email: new FormControl('', [Validators.required , Validators.email]),
password: new FormControl('', [Validators.required , Validators.minLength(4)]),
});
}
Alternatively, you can specify that loginForm can be undefined
export class LoginPageComponent implements OnInit {
loginForm: FormGroup | undefined; // краткая запись loginForm?: FormGroup;
ngOnInit(): void {
this.loginForm = new FormGroup({
email: new FormControl('', [Validators.required , Validators.email]),
password: new FormControl('', [Validators.required , Validators.minLength(4)]),
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question