Javascript I';我无法访问我的异步验证器,因为它进入了"__分区“U符号”;对象
我制作了这个异步验证器Javascript I';我无法访问我的异步验证器,因为它进入了"__分区“U符号”;对象,javascript,angular,typescript,promise,Javascript,Angular,Typescript,Promise,我制作了这个异步验证器 export class PasswordsValidators{ static oldPasswordMatch(control: AbstractControl) : Promise<ValidationErrors> | null { return new Promise((resolve) => { if(control.value !== "1234") resol
export class PasswordsValidators{
static oldPasswordMatch(control: AbstractControl) : Promise<ValidationErrors> | null {
return new Promise((resolve) => {
if(control.value !== "1234")
resolve({oldPasswordInvalid:true})
else
resolve(null);
});
}
}
这是前端
<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
])
})
你好。。也许这不对。。但我认为你在输入中遗漏了一些东西。。将文本框的值传递给验证。。因此它无法获取值并抛出错误(因为它被标记为必需的)。。也许试着在你的输入中加入[ngValue]或[ngModel]。嘿,谢谢,它起作用了。不过有一件事。第一次就不行了。一旦你转移了注意力,它就会起作用。是的,这是角度验证器的预期行为。很高兴这有帮助。
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
])
})