DEV Community

Cover image for Two-way bind a Signal Input object value with [(ngModel)]

Two-way bind a Signal Input object value with [(ngModel)]

Florian Spier on February 15, 2025

Recently, I encountered this challenge... Refactor a form component to Angular Signals. The old form component works like this: The form data c...
Collapse
 
anthonyikeda profile image
Anthony Ikeda

Just a question, but how is this better than the FormBuilder? I don’t see the comparison in this article.

Collapse
 
spierala profile image
Florian Spier

Reactive Forms are indeed not covered in the blog post, the focus is mutating the form data with ngModel which is only available in Template Driven Forms.

But still, I was curious what the setup would look like with Reactive Forms: stackblitz.com/edit/stackblitz-sta...

I believe that you have to use effect to update the form value with FormGroup.setValue.

Collapse
 
anthonyikeda profile image
Anthony Ikeda

Yeah that’s cool. I just think opening with the fact this is about Template Driven forms would have avoided the confusion. :)

Collapse
 
fyodorio profile image
Fyodor

Probably because it's a separate topic and it's a matter of opinion. Template-driven forms are much simpler and straightforward. Reactive forms are more robust and manageable. You do you, basically.

Collapse
 
spierala profile image
Florian Spier

We try to avoid Reactive Forms as much as possible.

Too much boilerplate for almost no gain.

Template Forms are Reactive Forms under the hood. So you get almost the same functionality.

Prefer Template-Driven Forms | Ward Bell | ng-conf 2021

Thread Thread
 
anthonyikeda profile image
Anthony Ikeda

I will watch the video later today, however while I’m not a super user of Angular I find the order that ReactiveForms FormGroups brings is where I appreciate it more. Having to manage individual fields feels messy to me when working on larger input systems - especially delving into Angular Material and Steppers but I’m more a PoC person :)

Collapse
 
anthonyikeda profile image
Anthony Ikeda

Yup see my response about opening with it being about template driven forms above.

Collapse
 
avareto profile image
Andreas Kuhlmann

Signals are not an improvement here. They make thinks more complicated and require more code.

Collapse
 
omergronich profile image
Omer Gronich • Edited

All things considered I think linkedSignal is a better option. I don't like opting out of signals and like @oz mentioned not everything can be cloned.

Some of the boilerplate can be reduced with a directive and a model input:

stackblitz.com/edit/stackblitz-sta...

Collapse
 
jwess profile image
Joel

Is there any way to work the new "model signal" into the mix? I guess maybe not since you're looking to work with properties of an object?

Collapse
 
spierala profile image
Florian Spier • Edited

You could replace the user input and the save output with Input model.

Input model is meant to sync data between child and parent component and vice versa: angular.dev/guide/components/input...

The challenges regarding the usage of [(ngModel)] will be the same also with Input model. I assume that you can apply the same solutions from the blog post.

Collapse
 
santoshyadavdev profile image
Santosh Yadav

Great explanation

Collapse
 
oz profile image
Evgeniy OZ

Some things just can not be cloned (because of circular references, functions inside, or links to DOM nodes). FormGroup, FormControl, for example.

Option with linkedSignal is the way to go.

Collapse
 
spierala profile image
Florian Spier

The cloning is restricted to the form data (the user Object) which is manipulated with ngModel.