How to add value to a form from code

How can I add value to a form from the code? I’ve tried to use AbstractControl.value but it has only getter. Thanks.

Code example:

<form [formGroup]="customer">
    <ion-item [class.error]="!FULL_NAME.valid && FULL_NAME.touched">
      <ion-label floating>{{'FULL_NAME' | translate:{value: param} }}</ion-label>
      <ion-input type="text" [formControl]="FULL_NAME"></ion-input>
    </ion-item>

    <div *ngIf="FULL_NAME.hasError('required') && FULL_NAME.touched" class="error-box">{{'EMPTY_WARNING' | translate:{value: param} }}</div>
</form>

................
customer: ControlGroup;
FULL_NAME: AbstractControl;
................
this.customer = fb.group({
    'FULL_NAME': ['', Validators.compose([Validators.required])]
});

this.FULL_NAME = this.customer.controls['FULL_NAME'];

`

Do you mean setting the value of the input from the controller and not from the HTML template with <input value="somevaluehere">?

Yeap. I need to set value from ts code and not from HTML.

As it happens I needed something similar for an issue yesterday. Can you not pass an array through to the form and iterate with a ngFor loop. I had to do this for editing a form that user had previously submitted. It was a PIA but got the job done.

Im all ears for a better way to accomplish this.

Share a bit of your code and what you’re using, this should be trivial.

Done. Check post out.

Note that this is NOT an ionic question techincally it is Angular2 on stackoverflow you’d get help quicker.

This probably already has the answer, it seems that your way of getting the control is fine, you could try to use this.FULL_NAME.updateValue(newValue)

1 Like

If you’re on RC5 try the last answer by Angular Academy

1 Like