您好我试图学习角度,当我尝试使用验证器时我得到错误,我收到错误: 无法读取null的属性'value'
这是我的组件:
import {Component} from 'angular2/core';
import {ControlGroup , Control, Validators, FormBuilder} from 'angular2/common';
import {CustomValidators} from './custom.validators4';
@Component({
selector : 'contact',
templateUrl : 'app/form.template4.html'
})
export class FormComponent4{
form1 : ControlGroup;
constructor(fb : FormBuilder){
this.form1 = fb.group({
oldpass : ['', Validators.required],
newpass : ['', Validators.required],
repass : ['', Validators.compose([ Validators.required , CustomValidators.retypeMatch ])]
});
}
onSubmit(){
this.form1.find('oldpass').setErrors({
incorrect : true
})
}
}
以下是我的CustomValidators组件:
import {ControlGroup} from 'angular2/common';
export class CustomValidators{
static retypeMatch(group : ControlGroup){
var pass = group.find('newpass').value;
var retype = group.find('repass').value;
if(pass == '' || retype == '')
return null;
if(pass != retype)
return {retypeMatch : true};
return null;
}
}