来自Angular docs:
const form = new FormGroup({
password: new FormControl('', Validators.minLength(2)),
passwordConfirm: new FormControl('', Validators.minLength(2)),
}, passwordMatchValidator);
function passwordMatchValidator(g: FormGroup) {
return g.get('password').value === g.get('passwordConfirm').value
? null : {'mismatch': true};
}
我不知道如何调用passwordMatchValidator
函数。试过像:
<div *ngIf="form.passwordMatchValidator">
但没有任何成功......
答案 0 :(得分:2)
如果密码不匹配,要显示包含错误消息的div,您只需:
<div *ngIf="form.hasError('mismatch')">