Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive forms in Angular: Two-way binding

I have a problem with Reactive forms. Updating the model class when the User changes the input is straight forward. But I need to do the opposite way: programmatically change the model, and see the changes in the HTML form.

The simplified code is:

this.user = new User();

And then:

this.form = this.fb.group({
      name: new FormControl(this.user.name, [Validators.required]),
      email: new FormControl(this.user.email, [Validators.required])    
    });

Assume that it is working fine. If the user updates the name in the GUI the this.user.name is being correctly updated.

Assume that I have added some logic to User class, that populates the email based on the entered name. Every time the name changes, the email field is auto-generated as 'name'@gmail.com

Unfortunately the generated email is not visible in the GUI. How to achieve that?

How to notify the FormGroup about changes in the User class?

I had two ideas:

1) Add ([ngModel]) - and it worked - but I feel that it is a bad idea to mix Reactive Forms with ngModel

2) Add the following code, after the form is created:

this.form.controls["name"].valueChanges.distinctUntilChanged().subscribe(form => {
    this.form.patchValue({email: this.model.email});
});

But it does not look clean. Is there any other option?

like image 846
Artur Avatar asked Jun 10 '18 11:06

Artur


People also ask

Can we use ngModel in reactive forms Angular?

Use with ngModel is deprecatedlink Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and is scheduled for removal in a future version of Angular.

What are reactive forms in Angular?

Reactive forms provide a model-driven approach to handling form inputs whose values change over time. This guide shows you how to create and update a basic form control, progress to using multiple controls in a group, validate form values, and create dynamic forms where you can add or remove controls at run time.

What is 2 way data binding in Angular?

The two-way data binding in Angular is used to display information to the end user and allows the end user to make changes to the underlying data using the UI. This makes a two-way connection between the view (the template) and the component class.

What is the difference between template driven and reactive forms?

Template Driven Forms are based only on template directives, while Reactive forms are defined programmatically at the level of the component class. Reactive Forms are a better default choice for new applications, as they are more powerful and easier to use.


1 Answers

That's just not how reactive forms are intended to work.

With reactive forms, the form is not bound to your model, it has its own model, which you can see if you look at "form.value".

The value that you pass to the form control as you are creating it is not bound, it's just used as an initial value for the form control.

The intention is that the user fills out the form, validations happen dynamically (that's the reactive part), and then when they submit the form, you transfer the form's value to your model, all at once.

That said, if you want to update the value of a form control, patchValue is indeed the correct way to do it.

like image 71
GreyBeardedGeek Avatar answered Oct 05 '22 20:10

GreyBeardedGeek