| 
									
										
										
										
											2019-12-02 12:47:19 -08:00
										 |  |  |  | # Frequently-used modules
 | 
					
						
							| 
									
										
										
										
											2017-07-04 10:58:20 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | An Angular app needs at least one module that serves as the root module. | 
					
						
							|  |  |  |  | As you add features to your app, you can add them in modules. | 
					
						
							|  |  |  |  | The following are frequently used Angular modules with examples | 
					
						
							|  |  |  |  | of some of the things they contain: | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | <table> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  <tr> | 
					
						
							|  |  |  |  |    <th style="vertical-align: top"> | 
					
						
							|  |  |  |  |      NgModule | 
					
						
							|  |  |  |  |    </th> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |    <th style="vertical-align: top"> | 
					
						
							|  |  |  |  |      Import it from | 
					
						
							|  |  |  |  |    </th> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |    <th style="vertical-align: top"> | 
					
						
							|  |  |  |  |      Why you use it | 
					
						
							|  |  |  |  |    </th> | 
					
						
							|  |  |  |  |  </tr> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  <tr> | 
					
						
							|  |  |  |  |    <td><code>BrowserModule</code></td> | 
					
						
							|  |  |  |  |    <td><code>@angular/platform-browser</code></td> | 
					
						
							|  |  |  |  |    <td>When you want to run your app in a browser</td> | 
					
						
							|  |  |  |  |  </tr> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  <tr> | 
					
						
							|  |  |  |  |    <td><code>CommonModule</code></td> | 
					
						
							|  |  |  |  |    <td><code>@angular/common</code></td> | 
					
						
							|  |  |  |  |    <td>When you want to use <code>NgIf</code>, <code>NgFor</code></td> | 
					
						
							|  |  |  |  |  </tr> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  <tr> | 
					
						
							|  |  |  |  |    <td><code>FormsModule</code></td> | 
					
						
							|  |  |  |  |    <td><code>@angular/forms</code></td> | 
					
						
							| 
									
										
										
										
											2018-06-02 10:18:11 +02:00
										 |  |  |  |    <td>When you want to build template driven forms (includes <code>NgModel</code>)</td> | 
					
						
							| 
									
										
										
										
											2017-07-04 10:58:20 -04:00
										 |  |  |  |  </tr> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  <tr> | 
					
						
							|  |  |  |  |    <td><code>ReactiveFormsModule</code></td> | 
					
						
							|  |  |  |  |    <td><code>@angular/forms</code></td> | 
					
						
							| 
									
										
										
										
											2018-06-02 10:18:11 +02:00
										 |  |  |  |    <td>When you want to build reactive forms</td> | 
					
						
							| 
									
										
										
										
											2017-07-04 10:58:20 -04:00
										 |  |  |  |  </tr> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  <tr> | 
					
						
							|  |  |  |  |    <td><code>RouterModule</code></td> | 
					
						
							| 
									
										
										
										
											2018-01-17 14:57:43 +02:00
										 |  |  |  |    <td><code>@angular/router</code></td> | 
					
						
							| 
									
										
										
										
											2018-06-02 10:18:11 +02:00
										 |  |  |  |    <td>When you want to use <code>RouterLink</code>, <code>.forRoot()</code>, and <code>.forChild()</code></td> | 
					
						
							| 
									
										
										
										
											2017-07-04 10:58:20 -04:00
										 |  |  |  |  </tr> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  <tr> | 
					
						
							| 
									
										
										
										
											2018-01-17 14:57:43 +02:00
										 |  |  |  |    <td><code>HttpClientModule</code></td> | 
					
						
							|  |  |  |  |    <td><code>@angular/common/http</code></td> | 
					
						
							| 
									
										
										
										
											2018-06-02 10:18:11 +02:00
										 |  |  |  |    <td>When you want to talk to a server</td> | 
					
						
							| 
									
										
										
										
											2017-07-04 10:58:20 -04:00
										 |  |  |  |  </tr> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | </table> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## Importing modules
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | When you use these Angular modules, import them in `AppModule`, | 
					
						
							|  |  |  |  | or your feature module as appropriate, and list them in the `@NgModule` | 
					
						
							| 
									
										
										
										
											2018-10-16 14:20:56 -06:00
										 |  |  |  | `imports` array. For example, in the basic app generated by the [Angular CLI](cli), | 
					
						
							| 
									
										
										
										
											2017-07-04 10:58:20 -04:00
										 |  |  |  | `BrowserModule` is the first import at the top of the `AppModule`, | 
					
						
							| 
									
										
										
										
											2018-01-17 14:57:43 +02:00
										 |  |  |  | `app.module.ts`. | 
					
						
							| 
									
										
										
										
											2017-07-04 10:58:20 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 14:57:43 +02:00
										 |  |  |  | ```typescript | 
					
						
							| 
									
										
										
										
											2017-07-04 10:58:20 -04:00
										 |  |  |  | /* import modules so that AppModule can access them */ | 
					
						
							|  |  |  |  | import { BrowserModule } from '@angular/platform-browser'; | 
					
						
							|  |  |  |  | import { NgModule } from '@angular/core'; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import { AppComponent } from './app.component'; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | @NgModule({ | 
					
						
							| 
									
										
										
										
											2018-01-17 14:57:43 +02:00
										 |  |  |  |   declarations: [ | 
					
						
							|  |  |  |  |     AppComponent | 
					
						
							|  |  |  |  |   ], | 
					
						
							|  |  |  |  |   imports: [ /* add modules here so Angular knows to use them */ | 
					
						
							|  |  |  |  |     BrowserModule, | 
					
						
							|  |  |  |  |   ], | 
					
						
							|  |  |  |  |   providers: [], | 
					
						
							|  |  |  |  |   bootstrap: [AppComponent] | 
					
						
							| 
									
										
										
										
											2017-07-04 10:58:20 -04:00
										 |  |  |  | }) | 
					
						
							|  |  |  |  | export class AppModule { } | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | The imports at the top of the array are JavaScript import statements | 
					
						
							|  |  |  |  | while the `imports` array within `@NgModule` is Angular specific. | 
					
						
							|  |  |  |  | For more information on the difference, see [JavaScript Modules vs. NgModules](guide/ngmodule-vs-jsmodule). | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## `BrowserModule` and `CommonModule`
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | `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`. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | For apps that run in the browser, import `BrowserModule` in the | 
					
						
							|  |  |  |  | root `AppModule` because it provides services that are essential | 
					
						
							|  |  |  |  | to launch and run a browser app. `BrowserModule`’s providers | 
					
						
							|  |  |  |  | are for the whole app so it should only be in the root module, | 
					
						
							|  |  |  |  | not in feature modules. Feature modules only need the common | 
					
						
							|  |  |  |  | directives in `CommonModule`; they don’t need to re-install app-wide providers. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | If you do import `BrowserModule` into a lazy loaded feature module, | 
					
						
							|  |  |  |  | Angular returns an error telling you to use `CommonModule` instead. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 14:47:51 -08:00
										 |  |  |  | <div class="lightbox"> | 
					
						
							|  |  |  |  |   <img src="generated/images/guide/frequent-ngmodules/browser-module-error.gif" width=750 alt="BrowserModule error"> | 
					
						
							|  |  |  |  | </div> | 
					
						
							| 
									
										
										
										
											2017-07-04 10:58:20 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | <hr /> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## More on NgModules
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | You may also be interested in the following: | 
					
						
							|  |  |  |  | * [Bootstrapping](guide/bootstrapping). | 
					
						
							|  |  |  |  | * [NgModules](guide/ngmodules). | 
					
						
							|  |  |  |  | * [JavaScript Modules vs. NgModules](guide/ngmodule-vs-jsmodule). |