I can imagine following approaches to exchange Data between multi step forms:
1) Create a component for each form step and exchange data between components over @input, @output (e.g. you cannot change from step5 to 2)
2) Use the new property data
in the new router (see here) (e.g. you cannot change from step5 to 2))
3) A shared Service (Dependency Injection) to store data (Component Interaction) (e.g. you can change from step5 to 2)
4) New rudiments with @ngrx/store (not really experienced yet)
Can you give some "gained experience values", what do you use and why?
Why not use session storage? For instance you can use this static helper class (TypeScript):
And using above class, you can put your object with multi-steps-forms data and share it (idea is similar like for 'session helper' in many backend frameworks like e.g. php laravel).
The other approach is to create Singleton service. It can look like that (in very simple from for sake of clarity) (I not test below code, I do it from head):
And then in your main file where you bootstrap application:
And the last step - critical: When you want to use you singleton service in your component - don't put int in providers section (this is due to angular2 DI behavior - read above link about singleton services). Example below for go from form step 2 to step 3:
It should looks like that.
See my edit below.
Using
SessionStorage
is not strictly the 'angular' way to approach this in my opinion—a shared service is the way to go. Implementing routing between steps would be even better (as each component can have its own form and different logic as you see fit:The service
multistep.service
can hold the model and implement logic for components:Good luck.
EDIT 12/6/2016
Actually, now having worked with the form API for a while I don't believe my previous answer is the best way to achieve this.
A preferrable approach is to create a top level
FormGroup
which has each step in your multistep form as it's ownFormControl
(either a FormGroup or a FormArray) under it'scontrols
property. The top level form in such a case would be the single-source of truth for the form's state, and each step on creation (ngOnInit / constructor) would be able to read data for its respective step from the top levelFormGroup
. See the pseudocode:Therefore, the state of the form and each step is kept exactly where it should be—in the form itself!