Angular reactive forms, adding and removing fields?

前端 未结 4 901
孤街浪徒
孤街浪徒 2021-02-14 10:27

while building crud app in angular 5 I\'ve come across with a question, how can I use the same form builder but change what form controls I get depending on what I want, adding

4条回答
  •  故里飘歌
    2021-02-14 11:11

    addControl RemoveControl using u can hide and show your Fields.

    {{ projectData?.description }}

    Add control:

    editField(this.formControlKeys.description){
            this.detailForm.addControl('description', new FormControl(''));
            this.detailForm.controls['description'].setValue(this.projectData.description);
    }
    

    remove control:

     removeDescriptionControl() {
        this.detailForm.removeControl('description');
      }
    

    create form group first:

     this.detailForm = this.formBuilder.group({
        });
    

    set formControlKeys:

    formControlKeys = {
        description: 'description'
      };
    

提交回复
热议问题