Angular子模块中input无法绑定ngModelOptions问题求助
Got it, let's break down why you're hitting this error and how to fix it right away:
Root Cause
Your SharedModule imports FormsModule and ReactiveFormsModule, but it doesn't export them. In Angular, when you import a module into another module, only the components, directives, and modules explicitly marked as exports from the imported module are available to the importing module. Since ngModelOptions is a directive provided by FormsModule, your submodule can't access it unless SharedModule makes that module available via exports.
Step-by-Step Fix
- Update your SharedModule to include the form modules in both
importsandexports:
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; @NgModule({ imports: [ CommonModule, FormsModule, ReactiveFormsModule ], exports: [ CommonModule, FormsModule, // Make ngModel-related directives available to submodules ReactiveFormsModule // Make reactive form directives available to submodules ] }) export class SharedModule { }
- Verify your submodule setup (ensure it's importing
SharedModulecorrectly—you likely already do this, but it's a quick check):
import { NgModule } from '@angular/core'; import { SharedModule } from '../shared/shared.module'; import { YourProblemComponent } from './your-problem.component'; @NgModule({ imports: [ SharedModule ], declarations: [YourProblemComponent] }) export class YourSubModule { }
Quick Note on Mixed Form Usage
I spotted that your template is using both [formControl] (reactive forms) and ngModel with standalone: true. While this works in specific scenarios, it's generally cleaner to stick to either reactive forms or template-driven forms consistently to avoid confusion and edge cases. If you're already using a reactive form group, you might not need ngModel here at all—just rely on [formControl]="personGroup.get('medication')" directly.
内容的提问来源于stack exchange,提问作者Justin Castillo

