Я использую ReactiveForms
в Angular v7.
Шаблон
<div class="login__container">
<div class="login__form">
<form novalidate [formGroup]="loginForm" (ngSubmit)="onSubmit()">
<div class="field">
<p class="control">
<input class="input" type="text" placeholder="Username" formControlName="username" [ngClass]="{ 'is-invalid': submitted && f.username.errors }">
</p>
</div>
<div class="field">
<p class="control">
<input class="input" type="password" placeholder="Password" formControlName="password" [ngClass]="{ 'is-invalid': submitted && f.password.errors }">
</p>
</div>
<div class="field">
<p class="control">
<button class="button is-success" [disabled]="!loginForm.valid">
Login
</button>
</p>
</div>
</form>
</div>
</div>
Компонент
import { Component, OnInit } from '@angular/core'
import { FormBuilder, FormGroup, Validators } from '@angular/forms'
@Component({
selector: 'app-login-component',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
loginForm: FormGroup
submitted = false
constructor(private fB: FormBuilder) {}
ngOnInit() {
this.loginForm = this.fB.group({
username: ['', Validators.required],
password: ['', Validators.required]
})
}
// convenience getter for easy access to form fields
get f() {
return this.loginForm.controls
}
onSubmit() {
this.submitted = true
// stop here if form is invalid
if (this.loginForm.invalid) {
return
}
console.log(this.loginForm.value)
}
}
Когда я иду по этому маршруту, у меня появляется ошибка консоли
ERROR TypeError: Cannot read property 'controls' of undefined
at Object.get [as f] (http://localhost:4200/main.js:257:35)
at http://localhost:4200/vendor.js:92667:9
at Array.forEach (<anonymous>)
at deepFreeze (http://localhost:4200/vendor.js:92664:33)
at http://localhost:4200/vendor.js:92670:7
at Array.forEach (<anonymous>)
at deepFreeze (http://localhost:4200/vendor.js:92664:33)
at http://localhost:4200/vendor.js:92670:7
at Array.forEach (<anonymous>)
at deepFreeze (http://localhost:4200/vendor.js:92664:33)
Я не могу понять, почему это происходит, кажется, что проверка вокруг кнопки отправки работает, поэтому я могу только предполагать, что на поля ссылаются правильно.Я подозреваю, что проблема возникает из-за удобства, установленного в компоненте, однако я не вижу, что с этим не так, поскольку автозаполнение предоставляет мне ожидаемые значения.