question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FormsModule not shared with Feature Modules

See original GitHub issue

I’m submitting a …

[x] bug report 
[ ] feature request
[ ] support request 

Current behavior Excuse my ignorance if this is expected behavior, but when I create a feature module (lazy loaded module using a route like { path: 'user', loadChildren: 'app/user/user.module#UserModule' }), I am required to import the FormsModule in the feature module even if it is already imported in the root/app module. It appears that this works for some modules (such as the BrowserModule), but not for the FormsModule. I get the error Can't bind to 'ngModel' since it isn't a known property of 'input'. when using [(ngModel)] in a form unless I actually import the FormsModule in the feature module.

Expected/desired behavior My understanding is that if you import a module in higher-level module, that the imported module should be available to all child/feature modules. This should work for FormsModule like it does for BrowserModule.

Reproduction of the problem You can see this behavior in this Plunk: http://plnkr.co/edit/yOX8nBsJdkLEG207kEhb?p=preview

I’ve modified the Heroes Plunk from the Routing example to add a feature module. Notice that if you click on the Feature link on the page that it attempts to load the feature module but logs an error in the console because it can’t find ngModel even though FormsModule is imported in /app/app.module.ts. However, if you uncomment line #15 in /app/feature/feature.module.ts (which imports FormsModule) then it works fine. Also notice that the *ngFor which comes from the BrowserModule works fine even though that is not loaded explicitly in the feature module (only in the app module).

What is the expected behavior? We shouldn’t have to import FormsModule in a feature module if it is already imported in higher module.

What is the motivation / use case for changing the behavior? Consistency with other imports.

Environment:

  • Angular version: RC5
  • Browser: Chrome 52.0.2743.116
  • Language: TypeScript

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
qdoublecommented, Sep 9, 2016

@jmcooper yes, NgModule providers get put into the root injector, so they do work differently than pipes/directives.

1reaction
jmcoopercommented, Sep 6, 2016

So why does ngFor (which comes from BrowserModule) not have to be imported?

On Sep 5, 2016 10:41 PM, “Anthony” notifications@github.com wrote:

@jmcooper https://github.com/jmcooper This is not a bug, it is expected behavior. The components, pipes and directives inside modules are not global and must be reimported inside feature modules.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular/issues/11365#issuecomment-244848203, or mute the thread https://github.com/notifications/unsubscribe-auth/ABb4k7YNAKyvkrOYFfflRlzhiCPtEiKCks5qnO70gaJpZM4J1bD1 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

FormsModule Not Working Properly in Different Module Than ...
Use angular 13.2.0; Use nx workspace; Setting path in tsconfig.json; Export NgForm from SharedModule (Not import in SharedModule, just export).
Read more >
Sharing modules - Angular
Creating shared modules allows you to organize and streamline your code. You can put commonly used directives, pipes, and components into one module...
Read more >
Using Shared Modules in Angular - Pluralsight
You have common modules, like ReactiveFormsModule or FormsModule ... a shared module and import this shared module into all feature modules.
Read more >
Why Feature module & Shared Module is important in Angular ...
Feature Module & Shared Module are ways to keep your angular application clean & streamline your code. Things will still work when such...
Read more >
Frequently used Modules - Angular - w3resource
BrowserModule's providers are for the whole app so it should only be in the root module, not in feature modules. Feature modules only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found