How to make a disabled reactive form Editable in A

2019-01-27 09:37发布

问题:

I am Using below code to create a form and make it readOnly, I am new to angular

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

I need to make it enabled and post edited data back to Json :

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

回答1:

Just use following code to enable your form .

this.comapnyIdentificationForm.enable();

To get a json object. Use following code:

this.comapnyIdentificationForm.value;

To fill-up your form with backend data use following code: this.comapnyIdentificationForm.patchValue(jsonData);



回答2:

You can use a directive too

@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">