Angular 2 conditional Validators.required?

前端 未结 5 1234
孤街浪徒
孤街浪徒 2020-12-09 01:33

How should I go about conditionally requiring a form field? I made a custom validator, but the conditional variables that I pass to the custom validator are static and remai

5条回答
  •  不知归路
    2020-12-09 02:17

    Simply use enable() and disable() methods on AbstractControl.

    this.myControl.valueChanges.subscribe(
          checked => {
            if (checked) {
              this.otherControl.enable();
            } else {
              this.otherControl.disable();
            }
          }
        );
    

提交回复
热议问题