0

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>
2
  • You really need to post more data then this. How you make it readonly for example Commented Dec 22, 2017 at 8:00
  • this.comapnyIdentificationForm.disable(), just at the end Commented Dec 22, 2017 at 8:09

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

4 Comments

I get data from JSON for different fields and if someone wants edit then need to send it back updated
Do you want to update your form with json data? Can you please properly describe so that I can help you better
On my tab 1st the data comes from backend json, and then i have a button for Edit this data , which enables the filed to be edited , and then if changes are made then need to resubmit the form data back to Backend json (post)
I update my answer for your use case. If still you need help then let me know.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.