Skip to content

Instantly share code, notes, and snippets.

@ghetolay
Last active April 16, 2017 22:09
Show Gist options
  • Save ghetolay/436f5d4760c219e57f0aae399db7b335 to your computer and use it in GitHub Desktop.
Save ghetolay/436f5d4760c219e57f0aae399db7b335 to your computer and use it in GitHub Desktop.
Question about Angular Forms and reusable components

Problem

At the moment it's not really easy to create reusable part of a form as a component like an address component for example. You either have to create a fake input by implementing ControlValueAccessor or pass the form down to the component using @Input and use a form directive on the component (kinda creating a sub-form).

Expected behavior

It'll be nice if we could make that easier. Like we would just have to create a component with some form directives in it. It'll then register to the parent form like for direct child form directive.
This plunker is a working example except we had to use some tricks with providers in order to make it work that way.

Cause

All form directives, like FormControlName, FormGroupName, NgModel etc..., are injecting the ControlContainer that way :

@Optional() @Host() @SkipSelf() parent: ControlContainer

If we were to remove @Host() this would work as the expected behavior but obviously this @Host() decorator is there for a purpose.

Question

The question is what's the constraint at the origin of the need to use @Host() on form directives ?
The context being, maybe we could satisfy that constraint and have the expected behavior with the @Until decorator proposal (directly or with a upgraded version).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment