这是我第一次尝试使用被动形式,不用说我没有走得太远。我收到此错误:ERROR TypeError: Cannot read property 'valid' of undefined
我在这里遗漏了什么吗?我一直关注this教程来学习。我对使用被动表格完全不熟悉所以任何建议都会有所帮助。
import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
@Component({
selector: 'app-login',
templateUrl: './login.component.html'
})
export class LoginComponent {
loginForm: FormGroup;
username = new FormControl('', Validators.required);
password = new FormControl('', Validators.required);
rememberme = new FormControl('');
constructor(
private fb: FormBuilder
) {
this.loginForm = fb.group({
'username': this.username,
'password': this.password,
'rememberme': this.rememberme
});
}
onSubmit() {
console.log('model-based form submitted');
console.log(this.loginForm);
return false;
}
}
`
<div id="leftPart"></div>
<div id="rightPart" class="container-fluid px-4 py-2">
<img src="assets/SVG/logo.svg" class="d-block w-50 mx-auto my-4" />
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
<div class="form-group">
<input class="form-control" placeholder="User Name" type='text' formControlName="username">
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" type='password' formControlName="password" />
</div>
<div class="d-flex justify-content-between align-items-center mb-3">
<div class="form-group m-0">
<div class="form-check m-0">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" formControlName="rememberme">
Remember me
</label>
</div>
</div>
<button class="btn btn-transparent" style="font-size: 1rem;">
<i class="fa fa-lock mr-2"></i>Forgot pwd?
</button>
</div>
<button class="btn btn-primary btn-rounded btn-lg btn-block" type="submit" [disabled]="!form.valid">Submit</button>
</form>
</div>
`
答案 0 :(得分:3)
您在
中命名表单loginForm
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
在submit
按钮上,您试图通过[disabled]="!form.valid"
停用该按钮,但Angular在您为您的表单form
命名时并不知道loginForm
是什么,因此它应该是[disabled]="!loginForm.valid"
答案 1 :(得分:0)
<强>打字稿
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
@Component({
selector: 'app-login',
templateUrl: './index.html'
})
export class LoginComponent implements OnInit {
private form: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.form = this.formBuilder.group({
userName: [null, Validators.required],
password: [null, Validators.required],
rememberMe: [null]
});
}
onSubmit() {
console.log('model-based form submitted');
console.log(this.form);
return false;
}
}
<强> HTML
<form class="form-horizontal" [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="form-group">
<input class="form-control" placeholder="User Name" type="text" id="userName" formControlName="userName">
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" type="password" id="password" formControlName="password">
</div>
<div class="d-flex justify-content-between align-items-center mb-3">
<div class="form-group">
<div class="form-check m-0">
<input class="form-check-input" type="checkbox" id="rememberMe" formControlName="rememberMe">Remember
</div>
</div>
<button class="btn btn-transparent">Forgot pwd?</button>
</div>
<button class="btn btn-primary" type="submit" [disabled]="!form.valid">Submit</button>
</form>