Created
October 6, 2023 12:05
-
-
Save dmorosinotto/8f558ae1b379d1c5feab410b859446d7 to your computer and use it in GitHub Desktop.
Helper to "compose" Reactive forms from different component -> use viewProviders: [provideControlContainer()],
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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