2
votes

I was reading through a really well written article about Angular's module loading here - https://medium.com/@cyrilletuzi/understanding-angular-modules-ngmodule-and-their-scopes-81e4ed6f7407

Since providers at feature modules' NgModule are actually injected globally (and eagerly), the article talks about the "forRoot" pattern used in modules like RouterModule.

I came across ReactiveFormsModule - https://angular.io/api/forms/ReactiveFormsModule - which has the FormBuilder provider, but it doesn't seem to use the forRoot pattern. Doesn't this mean every feature module using ReactiveFormsModule will end up re-creating the FormBuilder provider? Am I missing something?

1
read the article Avoiding common confusions with modules in Angular that expains the forRoot usage in depth - Max Koretskyi

1 Answers

3
votes

forRoot is only relevant for lazy-loaded modules. For eager loaded modules, providers are registered in the application root scope anyway.

If you have imported ReactiveFormsModule in lazy loaded modules, then a FormBuilder instance per lazy-loaded module would be created. It doesn't really matter thought, because FormBuilder doesn't have a state. So not being singleton doesn't make a difference for how FormBuilder works and because it doesn't have a state, it also doesn't make a difference memory-wise.