In you Plunker, I'm not sure why you have ngForm
<my-formwizard [form]="form" ngForm="form">
but it should not be. I think it can even create a whole new form. This needs to be removed. Once you remove this, you will run into another problem stating that there is no ControlContainer . ControlContainer is a FormGroupDirective ( [formGroup] ).
The problem is caused by formControlName . If you look at the source formControlName directive , and you look at the constructor, you will see that it requires a ControlContainer dependency. But not only that, but also the @Host 1 decoder, which means that it will only look for the ControlContainer in the host injector.
Honestly, Iām not quite sure that in this case the injector is used as the host injector, but it does not seem to belong to the form group directive. Perhaps because you have the components configured.
In the solution I found instead of formControlName , use [formControl] instead and just pass an instance of FormControl . FormControlDirective does not have this problem (where it needs a ControlContainer ), since it can be used offline.
So you could do it instead
<input [formControl]="nameCtrl" type="text" /> export class App { form: FormGroup; nameCtrl: FormControl; constructor(fb: FormBuilder) { this.nameCtrl = new FormControl(''); this.form = fb.group({ name: this.nameCtrl }); } }
This solves your problem. Here's the updated Plunker ,
See also:
- How to solve the creation of a complex form with a large number of custom components? for some design ideas.
1 - See Host and Visibility ... for a good read in this section
Paul samsotha
source share