In Angular, is it possible to have a linear stepper where the individual steps are separate components? For example:
<mat-horizontal-stepper [linear]="isLinear">
<mat-step [stepControl]="firstFormGroup" label="Some Form">
<first-component></first-component>
</mat-step>
<mat-step [stepControl]="secondFormGroup" label="Another Form">
<second-component></second-component>
</mat-step>
<mat-step [stepControl]="thirdFormGroup" label="Review">
<third-component></third-component>
</mat-step>
</mat-horizontal-stepper>
When I try this, I receive the following error upon hitting the matStepperNext
button:
TypeError: Cannot read property 'invalid' of undefined
.
Here is the solution that work for me.
Parent Ts file
Child component
Ok, I think I see a few issues:
foodFormGroup and pieFormGroup need to be defined in your tough-choice.component.ts file (Which, btw is misspelled in your example code)
Here's an example (from the docs) of how this might look:
Also, I don't see a module.ts file in your example. That's where you would want to import your @angular/material modules, not in the component files.
I'd suggest just giving the Angular Material documentation a once-over. https://material.angular.io/components/stepper/overview
You can use sub-forms to resolve it. I actually gave a talk a few months ago in Angular-UP conference about it: https://www.youtube.com/watch?v=sb7tgsNF2Jk
The idea, in general, is to create the form in the child component, inject the controlContainer using DI and setting the local form to be the controlContainer form.
Child Component:
Parent Component (html):
Parent Component (ts):
Anwsered in this post: angular - Angular Material 2 Stepper Controls
*Sorry if this is not the correct way of linking another post.