没有什么可以阻止您使用FormBuilder API 并传递AbstractControlOptions。您可以将这些选项传递给控件和/或组。
this.formGroup = this.fb.group({
id: ['', [Validators.required]],
name: ['', {validators: ..., asyncValidators: ..., updateOn: ...}],
}, { updateOn: 'submit' });
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators} from '@angular/forms'
@Component({
selector: 'my-app',
template: `
<form [formGroup]="formGroup" (ngSubmit)="onSubmit()">
<input type="text" placeholder="Id" formControlName="id" />
<input type="text" placeholder="Name" formControlName="name" />
<br/>
<br/>
<h3>Value</h3>
{{ formGroup.value | json }}
<h3>Valid</h3>
{{ formGroup.valid | json }}
<br/>
<br/>
<button type="submit" >Submit</button>
</form>
`
})
export class AppComponent {
formGroup: FormGroup
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.formGroup = this.fb.group({
id: ['', [Validators.required]],
name: [''],
}, { updateOn: 'submit' });
}
onSubmit() {
console.log('onSubmit')
console.log(this.formGroup.value)
console.log(this.formGroup.valid)
}
}