我有角形。
当我打开应用程序时,控制台在in fooValidation
没有我的情况下记录了四次。
in fooValidation
in fooValidation
in fooValidation
in fooValidation
为什么?这是设计的吗?如何使其仅在提交表单后或关注指定字段时执行?
import { Component } from "@angular/core";
import { FormGroup, FormControl } from '@angular/forms';
const fooValidation = () => {
console.log('in fooValidation ');
// check two fields here
return { error: true };
}
@Component({
selector: "my-app",
template: `
<form [formGroup]="profileForm">
<label>
First Name:
<input type="text" formControlName="firstName">
</label>
<label>
Last Name:
<input type="text" formControlName="lastName">
</label>
</form>
`
})
export class AppComponent {
name = "Angular";
profileForm = new FormGroup({
firstName: new FormControl(""),
lastName: new FormControl("")
}, {
validators: [fooValidation]
});
}