Angular2 Reactive Forms - Disable form control dynamically from condition

前端 未结 5 590
感情败类
感情败类 2021-02-07 08:05

I have a select control that I want to disable dynamically based on a condition:

this.activityForm = this.formBuilder.group({
  docType: [{ value: \'2\', disable         


        
5条回答
  •  滥情空心
    2021-02-07 08:20

    You have to handle select elements differently than you do other HTML elements. You will have to perform a reset when this.activeCategory changes.

    Something like this:

    this.activityForm.controls['docType'].reset({ value: '2', disabled: false });

    Of course, you will probably want to use the current value, rather than a hard coded '2'.

    Same sort of thing to disable it, if that need arises (and it probably will).

    this.activityForm.controls['docType'].reset({ value: '2', disabled: true });

    More information on ng form control reset.

提交回复
热议问题