| 
									
										
										
										
											2017-06-09 17:48:53 -04:00
										 |  |  | # Bootstrapping
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-11 18:05:20 -04:00
										 |  |  | An NgModule class describes how the application parts fit together. | 
					
						
							| 
									
										
										
										
											2017-07-16 11:32:22 +01:00
										 |  |  | Every application has at least one NgModule, the _root_ module | 
					
						
							|  |  |  | that you [bootstrap](#main) to launch the application. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | You can call it anything you want. The conventional name is `AppModule`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-11 15:36:40 +00:00
										 |  |  | The [setup](guide/setup) instructions produce a new project with the following minimal `AppModule`. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | You'll evolve this module as your application grows. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | <code-example path="setup/src/app/app.module.ts" title="src/app/app.module.ts" linenums="false"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | </code-example> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | After the `import` statements, you come to a class adorned with the | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | **`@NgModule`** [_decorator_](guide/glossary#decorator '"Decorator" explained'). | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-11 18:05:20 -04:00
										 |  |  | The `@NgModule` decorator identifies `AppModule` as an `NgModule` class. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | `@NgModule` takes a _metadata_ object that tells Angular how to compile and launch the application. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * **_imports_** — the `BrowserModule` that this and every application needs to run in a browser. | 
					
						
							|  |  |  | * **_declarations_** — the application's lone component, which is also ... | 
					
						
							|  |  |  | * **_bootstrap_** — the _root_ component that Angular creates and inserts into the `index.html` host web page. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-11 18:05:20 -04:00
										 |  |  | The [NgModules](guide/ngmodule) guide dives deeply into the details of NgModules. | 
					
						
							| 
									
										
										
										
											2017-07-16 11:32:22 +01:00
										 |  |  | All you need to know at the moment is a few basics about these three properties. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | {@a imports} | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | ### The _imports_ array
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-11 18:05:20 -04:00
										 |  |  | NgModules are a way to consolidate features that belong together into discrete units. | 
					
						
							| 
									
										
										
										
											2017-07-16 11:32:22 +01:00
										 |  |  | Many features of Angular itself are organized as NgModules. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | HTTP services are in the `HttpModule`. The router is in the `RouterModule`. | 
					
						
							|  |  |  | Eventually you may create a feature module. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Add a module to the `imports` array when the application requires its features. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _This_ application, like most applications, executes in a browser. | 
					
						
							|  |  |  | Every application that executes in a browser needs the `BrowserModule` from `@angular/platform-browser`. | 
					
						
							|  |  |  | So every such application includes the `BrowserModule` in its _root_ `AppModule`'s `imports` array. | 
					
						
							|  |  |  | Other guide and cookbook pages will tell you when you need to add additional modules to this array. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | <div class="alert is-important"> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | **Only `NgModule` classes** go in the `imports` array. Do not put any other kind of class in `imports`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | <div class="l-sub-section"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-11 18:05:20 -04:00
										 |  |  | The `import` statements at the top of the file and the NgModule's `imports` array | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | are unrelated and have completely different jobs. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The _JavaScript_ `import` statements give you access to symbols _exported_ by other files | 
					
						
							|  |  |  | so you can reference them within _this_ file. | 
					
						
							| 
									
										
										
										
											2017-06-09 17:48:53 -04:00
										 |  |  | You add `import` statements to almost every application file. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | They have nothing to do with Angular and Angular knows nothing about them. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The _module's_ `imports` array appears _exclusively_ in the `@NgModule` metadata object. | 
					
						
							| 
									
										
										
										
											2017-07-16 11:32:22 +01:00
										 |  |  | It tells Angular about specific _other_ NgModules—all of them classes decorated | 
					
						
							| 
									
										
										
										
											2017-07-11 18:05:20 -04:00
										 |  |  | with `@NgModule`—that the application needs to function properly. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | {@a declarations} | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | ### The _declarations_ array
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You tell Angular which components belong to the `AppModule` by listing it in the module's `declarations` array. | 
					
						
							|  |  |  | As you create more components, you'll add them to `declarations`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 17:48:53 -04:00
										 |  |  | You must declare _every_ component in an `NgModule` class. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | If you use a component without declaring it, you'll see a clear error message in the browser console. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 17:48:53 -04:00
										 |  |  | You'll learn to create two other kinds of classes — | 
					
						
							| 
									
										
										
										
											2017-03-11 15:36:40 +00:00
										 |  |  | [directives](guide/attribute-directives) and [pipes](guide/pipes) — | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | that you must also add to the `declarations` array. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | <div class="alert is-important"> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 17:48:53 -04:00
										 |  |  | **Only _declarables_** — _components_, _directives_ and _pipes_ — belong in the `declarations` array. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Do not put any other kind of class in `declarations`; _not_ `NgModule` classes, _not_ service classes, _not_ model classes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | {@a bootstrap-array} | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | ### The _bootstrap_ array
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-16 11:32:22 +01:00
										 |  |  | You launch the application by [_bootstrapping_](#main) the root `AppModule`. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Among other things, the _bootstrapping_ process creates the component(s) listed in the `bootstrap` array | 
					
						
							|  |  |  | and inserts each one into the browser DOM. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Each bootstrapped component is the base of its own tree of components. | 
					
						
							|  |  |  | Inserting a bootstrapped component usually triggers a cascade of component creations that fill out that tree. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 17:48:53 -04:00
										 |  |  | While you can put more than one component tree on a host web page, that's not typical. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Most applications have only one component tree and they bootstrap a single _root_ component. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You can call the one _root_ component anything you want but most developers call it `AppComponent`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Which brings us to the _bootstrapping_ process itself. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | {@a main} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Bootstrap in _main.ts_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | There are many ways to bootstrap an application. | 
					
						
							|  |  |  | The variations depend upon how you want to compile the application and where you want to run it. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In the beginning, you will compile the application dynamically with the _Just-in-Time (JIT)_ compiler | 
					
						
							|  |  |  | and you'll run it in a browser. You can learn about other options later. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 17:48:53 -04:00
										 |  |  | The recommended place to bootstrap a JIT-compiled browser application is in a separate file | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | in the `src` folder named `src/main.ts` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | <code-example path="setup/src/main.ts" title="src/main.ts" linenums="false"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | </code-example> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | This code creates a browser platform for dynamic (JIT) compilation and | 
					
						
							|  |  |  | bootstraps the `AppModule` described above. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The _bootstrapping_ process sets up the execution environment, | 
					
						
							| 
									
										
										
										
											2017-06-09 17:48:53 -04:00
										 |  |  | digs the _root_ `AppComponent` out of the module's `bootstrap` array, | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | creates an instance of the component and inserts it within the element tag identified by the component's `selector`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 17:48:53 -04:00
										 |  |  | The `AppComponent` selector — here and in most documentation samples — is `my-app` | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | so Angular looks for a `<my-app>` tag in the `index.html` like this one ... | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | <code-example path="setup/src/index.html" region="my-app" title="setup/src/index.html" linenums="false"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | </code-example> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | ... and displays the `AppComponent` there. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This file is very stable. Once you've set it up, you may never change it again. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-30 20:04:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | <l-main-section> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </l-main-section> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-11 18:05:20 -04:00
										 |  |  | ## More about NgModules
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Your initial app has only a single module, the _root_ module. | 
					
						
							|  |  |  | As your app grows, you'll consider subdividing it into multiple "feature" modules, | 
					
						
							| 
									
										
										
										
											2017-03-06 10:43:33 +00:00
										 |  |  | some of which can be loaded later ("lazy loaded") if and when the user chooses | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | to visit those features. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-11 18:05:20 -04:00
										 |  |  | When you're ready to explore these possibilities, visit the [NgModules](guide/ngmodule) guide. |