我正试图使用FormBuilder来减少表单组件上的大量代码,但是我无法使其正常工作,无论我做什么,我总是得到“无法读取属性”“无效的”“未定义的”,代码在下面,我遗漏了什么吗?
组件.html
<form [formGroup]="myForm" class="mt-3" (ngSubmit)="onSubmit()" novalidate>
<fieldset formGroupName="name">
<div class="form-group" [ngClass]="{'has-danger': firstName.invalid && (firstName.dirty || firstName.touched), 'has-success': firstName.valid && (firstName.dirty || firstName.touched)}">
<label>First Name</label>
<input type="text" class="form-control" formControlName="firstName">
</div>
<div class="form-group" [ngClass]="{'has-danger': lastName.invalid && (lastName.dirty || lastName.touched), 'has-success': lastName.valid && (lastName.dirty || lastName.touched)}">
<label>Last Name</label>
<input type="text" class="form-control" formControlName="lastName">
</div>
</fieldset>
<div class="form-group" [ngClass]="{'has-danger': email.invalid && (email.dirty || email.touched), 'has-success': email.valid && (email.dirty || email.touched)}">
<label>Email</label>
<input type="email" class="form-control" formControlName="email">
</div>
<div class="form-group" [ngClass]="{'has-danger': password.invalid && (password.dirty || password.touched), 'has-success': password.valid && (password.dirty || password.touched)}">
<label>Password</label>
<input type="password" class="form-control" formControlName="password">
</div>
<div class="form-group" [ngClass]="{'has-danger': language.invalid && (language.dirty || language.touched), 'has-success': language.valid && (language.dirty || language.touched)}">
<label>Language</label>
<select class="form-control" formControlName="language">
<option value="">Please select a language</option>
<option *ngFor="let lang of langs" [value]="lang">{{lang}}</option>
</select>
</div>
<button type="submit" class="btn btn-primary" [disabled]="!myForm.valid">Submit</button>
组件
import { Component, OnInit, AfterViewInit, Pipe } from '@angular/core';
import { FormsModule, FormGroup, FormControl, Validators, ReactiveFormsModule, FormBuilder } from '@angular/forms';
export class ModelDrivenFormFormModelValidationComponent implements OnInit {
langs: string[] = ['English', 'French', 'German'];
myForm: FormGroup;
constructor(private fb: FormBuilder) {
this.createForm();
}
createForm() {
this.myForm = this.fb.group({
name: this.fb.group({
firstName: ['', [Validators.required]],
lastName: ['', [Validators.required]]
}),
email: ['', [Validators.required, Validators.pattern('[^ @]*@[^@]*')]],
password: ['', [Validators.required, Validators.minLength(8)]],
language: ''
});
}
ngOnInit() {}
}
最佳答案
组件模板试图在声明变量之前访问该变量。对所有表单字段使用safe navigation operator?.
,以避免出现这种情况。示例如下:
<div class="form-group" [ngClass]="{'has-danger': password?.invalid && (password?.dirty || password?.touched), 'has-success': password?.valid && (password?.dirty || password?.touched)}">
<label>Password</label>
<input type="password" class="form-control" formControlName="password">
</div>
关于angular - Angular formbuilder找不到formgroup,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46043134/