簡體   English   中英

如何使禁用的反應形式在Angular2中可編輯

[英]How to make a disabled reactive form Editable in Angular2

我正在使用下面的代碼創建表單並將其設置為只讀

createForm() {
        this.comapnyIdentificationForm = this.fb.group({
          businessName: ['', Validators.required ],
          adressPrimary: '',
          adressSecondary: '',
          city:'',
          state: '',
          zipCode: '',
          country: '',
          companyPhone: '',
          DUNS: ''
        });
         this.comapnyIdentificationForm.disable();
      }

我需要啟用它,並將編輯后的數據發布回Json:

<button type="button"  (click)="EditData()" class="btn modal-btn btn-default">Edit</button>

只需使用以下代碼即可啟用您的表單。

this.comapnyIdentificationForm.enable();

獲取一個json對象。 使用以下代碼:

this.comapnyIdentificationForm.value;

要使用后端數據填寫表單,請使用以下代碼:this.comapnyIdentificationForm.patchValue(jsonData);

您也可以使用指令

@Directive({
  selector: '[enableControl]'
})
export class EnableControlDirective {
  @Input() set enableControl( condition : boolean ) {
    if (condition)
        this.ngControl.control.enable();
    else
      this.ngControl.control.disable();
  }

  constructor(private ngControl : NgControl ) {}
}

//use
<input class="form-control " [enableControl]="condition">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM