Skip to content

Instantly share code, notes, and snippets.

@dmorosinotto
Created October 6, 2023 12:05
Show Gist options
  • Save dmorosinotto/8f558ae1b379d1c5feab410b859446d7 to your computer and use it in GitHub Desktop.
Save dmorosinotto/8f558ae1b379d1c5feab410b859446d7 to your computer and use it in GitHub Desktop.
Helper to "compose" Reactive forms from different component -> use viewProviders: [provideControlContainer()],
//ORIGINAL CODE BY Nevzat Topçu @nevzatopcu
//STACKBLIZ SAMPLE https://stackblitz.com/edit/angular-form-share-elements?file=src%2Fmain.ts
//READ MORE HERE: https://nevzatopcu.medium.com/angular-child-components-with-reactive-forms-fbf4563b304c
import { Optional, SkipSelf, Provider } from '@angular/core';
import { ControlContainer } from '@angular/forms';
const containerFactory = (container: ControlContainer): ControlContainer => {
if (!container) {
throw new Error('I need a FormGroup instance');
}
return container;
};
function provideControlContainer(): Provider[] {
return [
{
provide: ControlContainer,
deps: [[new SkipSelf(), new Optional(), ControlContainer]],
useFactory: containerFactory,
},
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment