我做了这个异步验证器
export class PasswordsValidators{
static oldPasswordMatch(control: AbstractControl) : Promise<ValidationErrors> | null {
return new Promise((resolve) => {
if(control.value !== "1234")
resolve({oldPasswordInvalid:true})
else
resolve(null);
});
}
}
但是无法获取
oldPasswordInvalid
变量,因为它位于这样的对象__zone_symbol__value
中。这是formGroup
form = new FormGroup({
oldPassword: new FormControl("",[
Validators.required,
PasswordsValidators.oldPasswordMatch
]),
newPassword: new FormControl("",[
Validators.required
]),
confirmPassword: new FormControl("",[
Validators.required
])
})
这是前端
<div class="form-group">
<label>Old Password</label>
<input
formControlName="oldPassword"
type="password"
class="form-control"
placeholder="Password">
<div class="alert alert-danger" *ngIf="oldPassword.invalid && oldPassword.touched">
<p *ngIf="oldPassword.errors.required"> Should be filled </p>
<p *ngIf="oldPassword.errors.oldPasswordInvalid">Didn't match</p>
</div>
</div>
有人可以解释一下,我在这里想念什么?
最佳答案
问题是您要同时传递异步验证器和同步验证器。解决方法如下:
form = new FormGroup({
oldPassword: new FormControl("",
[Validators.required],
[PasswordsValidators.oldPasswordMatch] // note that async validators are passed as the third parameter
),
newPassword: new FormControl("",[
Validators.required
]),
confirmPassword: new FormControl("",[
Validators.required
])
})
关于javascript - 我无法访问“__zone_symbol”对象中的异步验证器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48850054/