Angular 2 or 4 Reactive form set validation depend on another field value

Angular 2 or 4 Reactive form set validation depend on another field value

我正在尝试使用 Reactive Forms 模块在 Angular 2 中构建一个表单,并且我正在尝试设置表单中某个字段的验证,如果另一个字段是 select.
我是不是做错了什么?

ngOnInit() {
    this.martialForm = this.fb.group({
        'maritalStatus': [this.model.maritalStatus, [Validators.required]],
        'spouseTitle': [this.spouseModel.title, null]
    },{
         validator: this.validateIsSpouse
    })
}

validateIsSpouse(group: FormGroup) {
    if(this.model.maritalStatus == "01"){
        group.controls['spouseTitle'].setErrors({ isRequired: true });
    }

    return null;
}

自定义验证函数在导入语句之后立即编写,代码和验证器应采用以下方式:

// import statement

function validateIsSpouse(group: FormGroup) {
  let maritalStatus = group.get('maritalStatus').value;
  if(maritalStatus == "01"){
    return { isRequired: true };
  }
  return null;
}

// @Component

export class MyClass implements OnInit {

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.martialForm = this.fb.group({
      'maritalStatus': [this.model.maritalStatus, [Validators.required]],
      'spouseTitle': [this.spouseModel.title, null]
    },{ validator: validateIsSpouse });
  }

}