Asynchronous 角反应式异步操作

Asynchronous 角反应式异步操作,asynchronous,angular-reactive-forms,change-password,Asynchronous,Angular Reactive Forms,Change Password,当我试图构建一个用于创建新密码表单的响应表单时,我遇到了这个错误。我在下面提到了源代码,当我删除源代码部分时,没有错误,但没有错误,我的操作也无法正常工作。我想我必须在我的源代码中添加或删除一些东西,以获得所需的输出 main.ts:12 TypeError: Cannot read property 'value' of null at FormGroup.passwordShouldMatch [as validator] (password.validators.ts:18)

当我试图构建一个用于创建新密码表单的响应表单时,我遇到了这个错误。我在下面提到了源代码,当我删除源代码部分时,没有错误,但没有错误,我的操作也无法正常工作。我想我必须在我的源代码中添加或删除一些东西,以获得所需的输出

main.ts:12 TypeError: Cannot read property 'value' of null
    at FormGroup.passwordShouldMatch [as validator] (password.validators.ts:18)
    at FormGroup._runValidator (forms.js:4089)
    at FormGroup.updateValueAndValidity (forms.js:4050)
    at new FormGroup (forms.js:4927)
    at FormBuilder.group (forms.js:8924)
    at new ChangePasswordComponent (change-password.component.ts:15)
    at createClass (core.js:31987)
    at createDirectiveInstance (core.js:31807)
    at createViewNodes (core.js:44210)
    at callViewAction (core.js:44660)






  static passwordShouldMatch(control : AbstractControl) {
        let newPassword = control.get('newPassowrd');
        let confirmPassword = control.get('confirmPassowrd');

        if (newPassword.value !== confirmPassword.value){
            return { passwordShouldMatch:true };

                return null;
        }
    }

由于您没有添加任何代码片段,我认为您的表单结构是这样的

this.fb.group({
  newPassowrd: [''],
  confirmPassowrd: [''],
});
<form [formGroup]="form" novalidate (ngSubmit)="onSubmit(survey)">
    <input type="text" placeholder="Untitled form" formControlName="newPassowrd">
    <input type="text" placeholder="Untitled form" formControlName="confirmPassowrd">
    <span *ngIf="form.hasError('passwordShouldMatch')">not match</span>
</form>
在这里,您包括一个名为passwordShouldMatch的自定义验证函数,该函数看起来不错。因此,我假设您在将验证器设置为该表单组时出错

this.fb.group({
  newPassowrd: [''],
  confirmPassowrd: [''],
}, { validator: this.passwordShouldMatch}); 
这是为表单组设置验证函数的方式。在html中,您的表单应该是这样的

this.fb.group({
  newPassowrd: [''],
  confirmPassowrd: [''],
});
<form [formGroup]="form" novalidate (ngSubmit)="onSubmit(survey)">
    <input type="text" placeholder="Untitled form" formControlName="newPassowrd">
    <input type="text" placeholder="Untitled form" formControlName="confirmPassowrd">
    <span *ngIf="form.hasError('passwordShouldMatch')">not match</span>
</form>

不匹配

一切都应该这样。以下是工作版本的

共享组件文件的完整代码段以及模板。如果可能的话,在Stackblitz中创建一个最小的可复制应用程序。事实上,我拼错了密码,这就是原因。无论如何,谢谢你对我的支持。。很高兴听到您发现并解决了您的问题。但是,由于我在这里投入了一些时间,如果我的答案的内容对你有任何帮助,请随意投票。