Angular 2响应式表单动态字段报错:未找到undefined的组件工厂
Hey there! I know how frustrating this error can be when you swear you've already configured everything correctly. Let's walk through the most common issues that might be causing this, even after adding your component to entryComponents:
1. Double-check the NgModule scope
Make sure your form-input.component.ts is declared in the exact same NgModule where you added it to entryComponents (or in a module that's properly imported/exported):
- Verify that
FormInputComponentis listed in thedeclarationsarray of your module (it needs to be declared before it can be added toentryComponents). - If you're using lazy-loaded modules, don't put the dynamic component in the root module—add it to the lazy-loaded module's
declarationsandentryComponentsinstead.
2. Angular Version Gotchas
If you're on Angular 9 or newer, the entryComponents array is actually deprecated thanks to the Ivy compiler. Ivy automatically detects dynamic components, so:
- Try removing the
entryComponentsarray entirely from your module. - Clear your Angular cache with
ng serve --no-cacheorng build --cleanto ensure old build artifacts aren't causing conflicts.
3. Component Decorator & Import Mistakes
- Open
form-input.component.tsand double-check the@Componentdecorator: ensure theselector,templateUrl, orstyleUrlsdon't have typos (a wrong path can break component registration entirely). - In your
dynamic-form.component.ts, confirm that you've correctly importedFormInputComponentand that you're using the exact class name when callingcomponentFactoryResolver.resolveComponentFactory(FormInputComponent). A tiny typo here will make Angular fail to recognize the component.
4. Module Export/Import Issues
If your dynamic component lives in a separate shared module, make sure:
- The shared module both declares and exports
FormInputComponent. - The module containing
testing.component.tsordynamic-form.component.tsimports this shared module.
Quick Debugging Steps
- Temporarily comment out the dynamic component creation code—does the error disappear? If yes, the issue lies in how you're instantiating the component.
- Check your browser's dev tools for additional error messages—sometimes a secondary error will point to the real root cause.
If you can share snippets of your NgModule code, the dynamic component creation logic, or form-input.component.ts, we can narrow this down even further!
内容的提问来源于stack exchange,提问作者Стас Рябцев

