Velidan Velidan - 1 month ago 12
TypeScript Question

import from SharedModule FormModule to module that exported via SharedModule Angular 2

I have some AutocompleteModule that exported with SharedModule to whole App. In SharedModule I have exported FormsModule to whole App.

But when I tried to use ngModel in AutocompleteModule I got an error about ngModel. When I imported FormsModule to AutocompelteModule also It works fine.

But I have FormsModule exported in SharedModule. So the reason question. Can I import SharedModule to AutocompleteModule to receive access to FormsModule? (But I saw in doc that it is bad idea to do that cycle).

So question is: should I import FormsModule to AutocompelteModule aso or some better practice exists?
Thanks!

Answer

AutocompleteModule needs it's own FormsModule import. You may think that importing the SharedModule into the AutocompleteModule would do the trick, but it won't as you will get a circular dependency, causing it to fail.

Best option is to just directly import the FormsModule

@NgModule({
  imports: [ FormsModule ]
})
class AutocompleteModule {}

In SharedModule I have exported FormsModule to whole App

It doesn't work like that. Access to components, pipes, and directives don't get inherited. They need to be imported into the module that uses them, one way or another.

Comments