{ "id": "guide/frequent-ngmodules", "title": "Frequently-used modules", "contents": "\n\n\n
An Angular app needs at least one module that serves as the root module.\nAs you add features to your app, you can add them in modules.\nThe following are frequently used Angular modules with examples\nof some of the things they contain:
\n\n NgModule\n | \n\n Import it from\n | \n\n Why you use it\n | \n
---|---|---|
BrowserModule | \n @angular/platform-browser | \n When you want to run your app in a browser | \n
CommonModule | \n @angular/common | \n When you want to use NgIf , NgFor | \n
FormsModule | \n @angular/forms | \n When you want to build template driven forms (includes NgModel ) | \n
ReactiveFormsModule | \n @angular/forms | \n When you want to build reactive forms | \n
RouterModule | \n @angular/router | \n When you want to use RouterLink , .forRoot() , and .forChild() | \n
HttpClientModule | \n @angular/common/http | \n When you want to talk to a server | \n
When you use these Angular modules, import them in AppModule
,\nor your feature module as appropriate, and list them in the @NgModule
\nimports
array. For example, in the basic app generated by the Angular CLI,\nBrowserModule
is the first import at the top of the AppModule
,\napp.module.ts
.
The imports at the top of the array are JavaScript import statements\nwhile the imports
array within @NgModule
is Angular specific.\nFor more information on the difference, see JavaScript Modules vs. NgModules.
BrowserModule
and CommonModule
linkBrowserModule
imports CommonModule
, which contributes many common\ndirectives such as ngIf
and ngFor
. Additionally, BrowserModule
\nre-exports CommonModule
making all of its directives available\nto any module that imports BrowserModule
.
For apps that run in the browser, import BrowserModule
in the\nroot AppModule
because it provides services that are essential\nto launch and run a browser app. BrowserModule
’s providers\nare for the whole app so it should only be in the root module,\nnot in feature modules. Feature modules only need the common\ndirectives in CommonModule
; they don’t need to re-install app-wide providers.
If you do import BrowserModule
into a lazy loaded feature module,\nAngular returns an error telling you to use CommonModule
instead.
You may also be interested in the following:
\n\n\n \n