Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: ReactiveForm Updating Model

Tags:

angular

I have a reactive form which I would like to directly populate my model.

form.component.html

<form [formGroup]="personForm" (ngSubmit)="savePerson()">
    <md-card class="app-input-section">
        <md-input formControlName="personName" placeholder="Person Name"></md-input>
        ... many more fields
        <button md-raised-button color="primary">Save</button>
    </md-card>
</form>

form.component.ts

@Component({
    selector: 'person',
    template: 'form.component.html'
})

export class FormComponent implements OnInit {

    person: Person;
    formBuilder: FormBuilder;
    personForm: FormGroup;

    constructor(formBuilder: FormBuilder, personService: PersonService) {
        this.person = new Person();
        this.personForm = formBuilder.group({
            personName: [this.person.personName, Validators.required],
            ... many more properties for form
        });
    }

    ngOnInit() {
        console.log('ngOnInit() called');
    }

    savePerson() {
        this.person.personName = this.personForm.value.personName;
        ... many more properties set
        this.personService.savePersontoDB(this.person);
    }
}

In the savePerson() function I am having to copy the values from the personForm FormGroup to the Person object. For a handful of properties this is fine however if I have many properties this will be just another thing to manage. How can I simplify this code so that either:

  1. the personForm values are automatically copied to the Person object as the form values change.
  2. the personForm values are automatically copied to the Person object when the user presses the "save" button (which subsequently calls the save() function. By this, I mean not having to copy all of the individual form values to my model (Person) object.

What is the best way to make this happen? Is there some kind of helper I can use or is it simpler than this?

Many thanks

JT

like image 646
JT-Helsinki Avatar asked Sep 05 '16 08:09

JT-Helsinki


People also ask

Is NG model used in reactive forms?

ngModel or Template driven forms and reactive forms( model driven forms ) can be mixed together. for example, it's easy to read data without subscription when you use TDF and on the other hand, you can provide some validations using MDF.

What is the difference between SetValue and PatchValue?

SetValue Vs PatchValue The difference is that with setValue we must include all the controls, while with the patchValue you can exclude some controls.

What is FormBuilder in reactive forms?

The FormBuilder service is an injectable provider that is provided with the reactive forms module. We will inject this dependency by adding it to the component constructor. File name : employeeDetails-editor.component.ts. 1constructor(private fb: FormBuilder) { } typescript.

Which method is used to update the form values in Angular?

Angular reactive forms follow a model-driven approach to handle form input whose values can be changed over time. These are also known as model-driven forms.


1 Answers

In my app, I have done this:

 this.selectedPhysical = <Physical>this.physicalForm.value;

this maps the fields in the form ui to the underlying object. So you could:

this.person = <Person>this.personForm.value;
this.personService.savePersontoDB(this.person);
like image 52
John Baird Avatar answered Oct 14 '22 13:10

John Baird