Replies: 5 comments
-
Yeah, Joshua and Mike Pearson are known for exploring declarative patterns in Angular. I think it all comes down to who manages the logic of your form. For most forms, I only have to apply a bunch of validators. They are naturally assigned to the FormControl or FormGroup, and there's my logic. Now if you want to be quite reactive, you could pass I've seen some experiments with passing the formGroup to the Signal Store but I didn't like them to be honest. What you should also consider: We are expecting a Signal upgrade for the reactive forms in the next few months. I hope that it becomes much easier then. |
Beta Was this translation helpful? Give feedback.
-
I feel like you are singlehandedly helping me through this challenging yet exciting time in Angular development!! I fear your final comment is going to be the key to this which makes developing Angular apps very challenging at the moment as things are changing so fast. I'm leaning towards the pattern of including the FormGroup within the state as it seems to allow the store to encapsulate almost all of the form logic. I left the goBack() method in the component because it seemed less business logic related and purely UI. Obviously, I have reservations due to your own comment about disliking this approach and what might be released in the coming months. As this is a completely green field startup project the chances are any new Angular feature introductions will need to be retrospectively applied anyway. Just for discussion and critical review by any other interested parties I have included my code where I have included the FormGroup in the signalStore; - Signal Store
Component
Template
|
Beta Was this translation helpful? Give feedback.
-
Hey Andrew,
I meant that literally, like I would say, "I don't like chocolate ice cream." It doesn't mean that I wouldn't recommend it or that it is technically wrong. I don't like the style and would only use it if there is no better alternative. As I said, my forms tend to consist of many fields along some validators, and only logic for loading and submitting. In your case, you have more logic than just some validators. It is also a local state, and the current alternative would be to put everything back into the component, which I like even less ;) So, in your scenario, I'd say your approach is perfectly fine 👍 |
Beta Was this translation helpful? Give feedback.
-
I tried using it like this: Signal Store:
Component template:
But the Did you overcame something like this? |
Beta Was this translation helpful? Give feedback.
-
I've completely moved away from storing the FormGroup in the store. I create the FormGroup within the component itself and use an effect to trigger the creation. Something like the invalid flag above I would simply manage by creating a public signal on my component and update that using the FormGroup valueChanges event via an NGRX subscription. I got really hung up on not using Observables/NGRX and it transpires that sometimes its just the easiest most suitable thing to do. Same with effects you'll see a lot about "not using them" but they perform a valuable function when used in the correct manner. Hopefully Angular are working on a more "signally" approach to implementing ReactiveForms. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to settle on a pattern to use with my reactive forms and signalStore as my state management tool. I watched this video by Josh Morony signal slice which seems to suggest a pattern which could work but using signalStore as opposed to his own signalSlice utility.
Has anybody got any thoughts on this?
I've added my FormGroup into the signalStore state which seems to make most sense as it keeps the bulk of the business logic out of the component and in the store however, it is a bit of faff loading the control values in and out. However, that could be much less of an issue using Josh's approach of managing things with the whole form value not individual controls.
I'm going to have another crack now at setting my form up using signalStore with the FormGroup inside the store and trying to work with the form as a whole when it changes etc. and report back.
Beta Was this translation helpful? Give feedback.
All reactions