Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NgModule and FormModule importing on every module?

So I'm using RC5, and when embracing things seems to go pretty decently for now I ran into an issue which I'm not sure if I'm doing something wrong or is working as intended;

Before it was possible to make form directives etc. globally available. Nowadays we started using NgModule. It seems though that I can't use FormsModule and ReactiveFormsModule globally, either in AppModule nor in a SharedModule. I need to import it in every single module I create.

Is this as intended?

like image 822
Mathijs Segers Avatar asked Aug 23 '16 09:08

Mathijs Segers


People also ask

What should be imported BrowserModule or CommonModule?

Should I import BrowserModule or CommonModule ? link. The root application module, AppModule , of almost every browser application should import BrowserModule from @angular/platform-browser . BrowserModule provides services that are essential to launch and run a browser application.

Which Angular module automatically imports by BrowserModule the imported module has directives available like NgIf & NgFor?

BrowserModule and CommonModule link BrowserModule imports CommonModule , which contributes many common directives such as ngIf and ngFor . Additionally, BrowserModule re-exports CommonModule making all of its directives available to any module that imports BrowserModule .

What is an NgModule?

NgModules configure the injector and the compiler and help organize related things together. An NgModule is a class marked by the @NgModule decorator. @NgModule takes a metadata object that describes how to compile a component's template and how to create an injector at runtime.


2 Answers

According to this NgModule doc it is as intended,

  • so if you are using multiple NgModule heirarchy in your app, you will need to create a SharedModule and import/export all the global Modules/Components/Directives/Pipes in it.

  • then import this module in every Module that you want to share it with.


You can read the docs for more clarification.

like image 160
Ankit Singh Avatar answered Sep 19 '22 04:09

Ankit Singh


You should add FormsModule and ReactiveFormsModule to your SharedModule exports, then you need to import your SharedModule into your other modules. That way, you will be able to use desired directives globally.

like image 45
Stefan Svrkota Avatar answered Sep 21 '22 04:09

Stefan Svrkota