| 
									
										
										
										
											2016-02-05 23:27:06 -08:00
										 |  |  | include _util-fns | 
					
						
							| 
									
										
										
										
											2015-05-07 18:24:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-09-20 05:24:40 +02:00
										 |  |  |   Let's start from zero and build a super simple Angular application in JavaScript. | 
					
						
							| 
									
										
										
										
											2015-03-02 06:01:47 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 11:41:28 -07:00
										 |  |  | .callout.is-helpful | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  |   header Don't want JavaScript? | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-09-20 05:24:40 +02:00
										 |  |  |     Although we're getting started in JavaScript, you can also write Angular apps | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  |     in TypeScript and Dart by selecting either of those languages from the combo-box in the banner. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   ## See It Run! | 
					
						
							| 
									
										
										
										
											2015-07-20 11:41:28 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-30 21:00:52 -05:00
										 |  |  |   Running the <live-example></live-example> | 
					
						
							| 
									
										
										
										
											2016-09-20 05:24:40 +02:00
										 |  |  |   is the quickest way to see an Angular app come to life. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   Clicking that link fires up a browser, loads the sample in [plunker](http://plnkr.co/ "Plunker"), | 
					
						
							|  |  |  |   and displays a simple message: | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | figure.image-display | 
					
						
							| 
									
										
										
										
											2016-11-05 13:12:11 -07:00
										 |  |  |   img(src='/resources/images/devguide/quickstart/hello-angular.png' alt="Output of quickstart app") | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   Here is the file structure: | 
					
						
							| 
									
										
										
										
											2015-12-16 14:14:02 -08:00
										 |  |  | .filetree | 
					
						
							| 
									
										
										
										
											2016-09-20 05:24:40 +02:00
										 |  |  |   .file angular-quickstart | 
					
						
							| 
									
										
										
										
											2015-12-16 14:14:02 -08:00
										 |  |  |   .children | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |     .file src | 
					
						
							| 
									
										
										
										
											2015-12-16 14:14:02 -08:00
										 |  |  |     .children | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |       .file app | 
					
						
							|  |  |  |       .children | 
					
						
							|  |  |  |         .file app.component.js | 
					
						
							|  |  |  |         .file app.module.js | 
					
						
							| 
									
										
										
										
											2016-01-28 16:15:26 -08:00
										 |  |  |       .file main.js | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |       .file index.html | 
					
						
							|  |  |  |       .file styles.css | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   Functionally, it's an `index.html`, `styles.css` and three JavaScript files in an `app/` folder. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   We can handle that! | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Of course we won't build  many apps that only run in plunker.  | 
					
						
							|  |  |  |   Let's follow a process that's closer to what we'd do in real life. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   1. Set up our development environment | 
					
						
							|  |  |  |   1. Write the Angular root component for our app | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   1. Add an Angular Module | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   1. Bootstrap it to take control of the main web page | 
					
						
							|  |  |  |   1. Write the main page (`index.html`) | 
					
						
							| 
									
										
										
										
											2016-06-20 01:31:49 +01:00
										 |  |  |   1. Add some CSS (`styles.css`) | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							|  |  |  | .l-sub-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |     We really can build the QuickStart from scratch in five minutes  | 
					
						
							|  |  |  |     if we follow the instructions and ignore the commentary. | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     Most of us will be interested in the "why" as well as the "how" and that will take longer. | 
					
						
							|  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | .l-main-section | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   ## Development Environment | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2015-12-28 16:48:17 +00:00
										 |  |  |   We'll need a place to stand (the application project folder), some libraries  | 
					
						
							|  |  |  |   and your editor of choice. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   ### Create a new project folder | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | code-example(format=""). | 
					
						
							| 
									
										
										
										
											2016-09-20 05:24:40 +02:00
										 |  |  |   mkdir angular-quickstart | 
					
						
							|  |  |  |   cd    angular-quickstart | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   ### Add the libraries we need | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   We recommend the **npm** package manager for acquiring and managing our development libraries. | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | .l-sub-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |     Don't have npm? | 
					
						
							|  |  |  |     [Get it now](https://docs.npmjs.com/getting-started/installing-node "Installing Node.js and updating npm") | 
					
						
							|  |  |  |     because we're going to use it now and repeatedly throughout this documentation. | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   Add a **package.json** file to the project folder and copy/paste the following: | 
					
						
							|  |  |  | +makeJson('quickstart/js/package.1.json', null, 'package.json')(format=".")   | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | .l-sub-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |     Itching to know the details? We explain in the [appendix below](#package-json)   | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   Install these packages by opening a terminal window (command window in Windows) and | 
					
						
							|  |  |  |   running this npm command. | 
					
						
							|  |  |  | code-example(format=""). | 
					
						
							|  |  |  |   npm install | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  | .l-sub-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |     Scary <span style="color:red; font-weight: bold">error messages in red</span> may appear **during** install.  | 
					
						
							|  |  |  |     Ignore them. The install will succeed. See the [appendix below](#npm-errors) for more information. | 
					
						
							| 
									
										
										
										
											2015-03-02 06:01:47 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   ## Our First Angular Component | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   The *Component* is the most fundamental of Angular concepts.  | 
					
						
							|  |  |  |   A component manages a view - a piece of the web page where we display information | 
					
						
							|  |  |  |   to the user and respond to user feedback. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Technically, a component is a class that controls a view template. | 
					
						
							|  |  |  |   We'll write a lot of them as we build Angular apps. This is our first attempt | 
					
						
							|  |  |  |   so we'll keep it ridiculously simple. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   ### Create an application source sub-folder | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |   We like to keep our application code in a sub-folder off the root called `src/app/`. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   Execute the following command in the console window. | 
					
						
							|  |  |  | code-example(format=""). | 
					
						
							|  |  |  |   mkdir app | 
					
						
							|  |  |  |   cd    app | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   ### Add the component file | 
					
						
							|  |  |  |   Now add a file named **app.component.js** and paste the following lines: | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  | +makeExample('quickstart/js/src/app/app.component.js', null, 'src/app/app.component.js')(format=".") | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   We're creating a visual component named **`AppComponent`** by chaining the | 
					
						
							|  |  |  |   `Component` and `Class` methods that belong to the **global Angular core namespace, `ng.core`**. | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  | +makeExample('quickstart/js/src/app/app.component.js', 'ng-namespace-funcs', 'src/app/app.component.js ' + | 
					
						
							| 
									
										
										
										
											2015-12-20 08:31:48 -05:00
										 |  |  | '(component schema)')(format=".") | 
					
						
							| 
									
										
										
										
											2015-12-17 11:38:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   The **`Component`** method takes a configuration object with three | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   properties. The **`Class`** method is where we implement the component itself, | 
					
						
							|  |  |  |   giving it properties and methods that bind to the view and whatever | 
					
						
							|  |  |  |   behavior is appropriate for this part of the UI. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Let's review this file in detail. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ### Modules | 
					
						
							|  |  |  |   Angular apps are modular. They consist of many files each dedicated to a purpose. | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2015-12-20 08:31:48 -05:00
										 |  |  |   ES5 JavaScript doesn't have a native module system. | 
					
						
							|  |  |  |   There are several popular 3rd party module systems we could use. | 
					
						
							|  |  |  |   Instead, for simplicity and to avoid picking favorites, | 
					
						
							|  |  |  |   we'll create a single global namespace for our application. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   We'll call it `app` and we'll add all of our code artifacts to this one global object. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   We don't want to pollute the global namespace with anything else. | 
					
						
							|  |  |  |   So within each file we surround the code in an IIFE ("Immediately Invoked Function Expression"). | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  | +makeExample('quickstart/js/src/app/app.component.js', 'iife', 'src/app/app.component.js (IIFE)')(format=".") | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							|  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-12-20 08:31:48 -05:00
										 |  |  |   We pass the global `app` namespace object into the IIFE,  | 
					
						
							|  |  |  |   taking care to initialize it with an empty object if it doesn't yet exist. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Most application files *export* one thing by adding that thing to the `app` namespace. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   Our `app.component.js` file exports the `AppComponent`. | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  | +makeExample('quickstart/js/src/app/app.component.js', 'export', 'src/app/app.component.js (export)')(format=".") | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   A more sophisticated application would have child components that descended from | 
					
						
							|  |  |  |   `AppComponent` in a visual tree.  | 
					
						
							|  |  |  |   A more sophisticated app would have more files and modules, at least as many as it had components. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Quickstart isn't sophisticated; one component is all we need. | 
					
						
							|  |  |  |   Yet modules play a fundamental organizational role in even this small app. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Modules rely on other modules. In JavaScript Angular apps, when we need something | 
					
						
							|  |  |  |   provided by another module, we get it from the `app` object.  | 
					
						
							|  |  |  |   When another module needs to refer to `AppComponent`, it gets it from the `app.AppComponent` like this: | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  | +makeExample('quickstart/js/src/app/app.module.js', 'import', 'src/app/app.module.js (import)')(format=".") | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   Angular is also modular. It is a collection of library modules.  | 
					
						
							|  |  |  |   Each library is itself a module made up of several, related feature modules. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   When we need something from Angular, we use the `ng` object. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   ### The Class definition object | 
					
						
							|  |  |  |   At the bottom of the file is an empty, do-nothing class definition object for  | 
					
						
							|  |  |  |   our `AppComponent` class. | 
					
						
							|  |  |  |   When we're ready to build a substantive application,  | 
					
						
							|  |  |  |   we can expand this object with properties and application logic.  | 
					
						
							|  |  |  |   Our `AppComponent` class has nothing but an empty constructor because we  | 
					
						
							|  |  |  |   don't need it to do anything in this QuickStart. | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  | +makeExample('quickstart/js/src/app/app.component.js', 'class','app.component.js (class)')(format=".")   | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   ### The Component definition object | 
					
						
							|  |  |  |   `ng.core.Component()` tells Angular that this class definition object | 
					
						
							|  |  |  |    *is an Angular component*. | 
					
						
							|  |  |  |   The configuration object passed to the `ng.core.Component()` method has two | 
					
						
							|  |  |  |   fields, a `selector` and a `template`. | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  | +makeExample('quickstart/js/src/app/app.component.js', 'component','app.component.js (component)')(format=".")     | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   The `selector` specifies a simple CSS selector for a host HTML element named `my-app`. | 
					
						
							|  |  |  |   Angular creates and displays an instance of our `AppComponent` | 
					
						
							|  |  |  |   wherever it encounters a `my-app` element in the host HTML. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | .alert.is-helpful | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |     Remember the `my-app` selector! We'll need that information when we write our `index.html` | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   The `template` property holds the component's companion template. | 
					
						
							|  |  |  |   A template is a form of HTML that tells Angular how to render a view. | 
					
						
							| 
									
										
										
										
											2016-11-21 17:13:21 -08:00
										 |  |  |   Our template is a single line of HTML announcing "Hello Angular". | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   Now we need something to tell Angular to load this component. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   ### Add an NgModule | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-09 15:56:47 -07:00
										 |  |  |   Angular apps are composed of [Angular Modules](guide/ngmodule.html) that  | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   snuggly contain all our components and everything else we need for our app. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |   Create the `src/app/app.module.js` file with the following content: | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  | +makeExample('quickstart/js/src/app/app.module.js', null, 'src/app/app.module.js') | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | .l-sub-section | 
					
						
							|  |  |  |   :marked | 
					
						
							|  |  |  |     Read more about the `NgModule` configuration in the [appendix below](#ngmodule). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-28 16:15:26 -08:00
										 |  |  |   ### Bootstrap it! | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |   Add a new file , `main.js`, to the `src/app/` folder as follows: | 
					
						
							|  |  |  | +makeExample('quickstart/js/src/main.js', null, 'src/main.js')(format=".") | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   We need two things to launch the application: | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   1. Angular's `platformBrowserDynamic().bootstrapModule` function | 
					
						
							|  |  |  |   1. The application root module that we just wrote. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   We have them both in our 'namespaces'.  | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   Then we call `bootstrapModule`, passing in the **root app module**, `AppModule`. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   | 
					
						
							|  |  |  | .l-sub-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |     Learn why we need `bootstrapModule` from `ng.platformBrowserDynamic` | 
					
						
							|  |  |  |     and why we create a separate js files in the [appendix below](#main). | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   We've asked Angular to launch the app in a browser with our component at the root. | 
					
						
							|  |  |  |   Where will Angular put it?  | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | .l-main-section | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   ## Add the `index.html` | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Angular displays our application in a specific location on our `index.html`. | 
					
						
							|  |  |  |   It's time to create that file. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |   We won't put our `index.html` in the `src/app/` folder.  | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   We'll locate it **up one level, in the project root folder**. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | code-example(format=""). | 
					
						
							|  |  |  |   cd .. | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   Now create the`index.html` file and paste the following lines: | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  | +makeExample('quickstart/js/src/index.html', null, 'src/index.html')(format=".") | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   There are three noteworthy sections of HTML: | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-17 22:30:19 -08:00
										 |  |  |   1. We load the JavaScript libraries we need; learn about them [below](#libraries). | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 18:33:03 +02:00
										 |  |  |   2. We load our JavaScript files. | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   3. We add the `<my-app>` tag in the `<body>`. **This is where our app lives!** | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   When Angular calls the `bootstrapModule` function in `main.js`, | 
					
						
							|  |  |  |   it reads the `AppModule` metadata, sees that `AppComponent` is the bootstrap component,  | 
					
						
							|  |  |  |   finds the `my-app` selector, locates an element tag named `my-app`, | 
					
						
							|  |  |  |   and renders our application's view between those tags. | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-20 01:31:49 +01:00
										 |  |  | :marked | 
					
						
							|  |  |  |   ### Add some style | 
					
						
							|  |  |  |   Styles aren't essential but they're nice, and `index.html` assumes we have | 
					
						
							|  |  |  |   a stylesheet called `styles.css`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Create a `styles.css` file in the *#{_indexHtmlDir}* folder and start styling, perhaps with the minimal | 
					
						
							|  |  |  |   styles shown below. For the full set of master styles used by the documentation samples, | 
					
						
							| 
									
										
										
										
											2016-11-18 21:24:35 -08:00
										 |  |  |   see [styles.css](https://github.com/angular/angular.io/blob/master/public/docs/_examples/_boilerplate/styles.css). | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  | +makeExample('quickstart/js/src/styles.1.css', null, 'src/styles.css')(format=".") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |  ### Configure the server | 
					
						
							|  |  |  |   We're going to use a static server called **lite-server** that loads `index.html` in a browser | 
					
						
							|  |  |  |   and refreshes the browser when application files change. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   The static server will use the `bs-config.json` file as configuration. | 
					
						
							|  |  |  |   This files tells the server that `src/` is the base directory to serve, and that any | 
					
						
							|  |  |  |   requests to `node_modules/` should be routed to `node_modules/` in the root directory | 
					
						
							|  |  |  |   instead of `src/node_modules/` | 
					
						
							| 
									
										
										
										
											2016-06-20 01:31:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-03 13:50:44 -08:00
										 |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-06 02:09:21 -05:00
										 |  |  |   ## Run! | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   Open a terminal window and enter this command: | 
					
						
							|  |  |  | code-example(format=""). | 
					
						
							|  |  |  |   npm start | 
					
						
							|  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |   That command runs **lite-server**. | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   In a few moments, a browser tab should open and display | 
					
						
							| 
									
										
										
										
											2015-03-04 05:27:48 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | figure.image-display | 
					
						
							| 
									
										
										
										
											2016-11-05 13:12:11 -07:00
										 |  |  |   img(src='/resources/images/devguide/quickstart/hello-angular.png' alt="Output of quickstart app") | 
					
						
							| 
									
										
										
										
											2015-08-27 16:03:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   Congratulations!  We are in business. | 
					
						
							| 
									
										
										
										
											2015-03-04 22:48:32 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | .alert.is-helpful | 
					
						
							|  |  |  |   :marked | 
					
						
							|  |  |  |     If you see `Loading...` displayed instead, see the | 
					
						
							| 
									
										
										
										
											2016-01-17 22:30:19 -08:00
										 |  |  |     [Browser ES2015 support appendix](#es6support). | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   ### Make some changes | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2017-01-26 03:26:11 -08:00
										 |  |  |   Try changing the message to "Hello Angular!". | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   `lite-server` is watching, so it should detect the change, | 
					
						
							|  |  |  |   refresh the browser, and display the revised message. | 
					
						
							| 
									
										
										
										
											2015-03-04 22:48:32 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   It's a nifty way to develop an application! | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   We close the terminal window when we're done to terminate the server. | 
					
						
							| 
									
										
										
										
											2015-03-02 06:01:47 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   ## Final structure | 
					
						
							|  |  |  |   Our final project folder structure looks like this: | 
					
						
							| 
									
										
										
										
											2015-12-15 14:45:45 -08:00
										 |  |  | .filetree | 
					
						
							| 
									
										
										
										
											2016-09-20 05:24:40 +02:00
										 |  |  |   .file angular-quickstart | 
					
						
							| 
									
										
										
										
											2015-12-15 14:45:45 -08:00
										 |  |  |   .children | 
					
						
							|  |  |  |     .file node_modules | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |     .file src | 
					
						
							| 
									
										
										
										
											2015-12-15 14:45:45 -08:00
										 |  |  |     .children | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |       .file app | 
					
						
							|  |  |  |       .children | 
					
						
							|  |  |  |         .file app.component.js | 
					
						
							|  |  |  |         .file app.module.js | 
					
						
							| 
									
										
										
										
											2016-01-28 16:15:26 -08:00
										 |  |  |       .file main.js | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |       .file index.html | 
					
						
							|  |  |  |       .file styles.css | 
					
						
							|  |  |  |     .file bs-config.json | 
					
						
							| 
									
										
										
										
											2015-12-15 14:45:45 -08:00
										 |  |  |     .file package.json | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  |   And here are the files: | 
					
						
							|  |  |  | +makeTabs(` | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |     quickstart/js/src/app/app.component.js, | 
					
						
							|  |  |  |     quickstart/js/src/app/app.module.js, | 
					
						
							|  |  |  |     quickstart/js/src/main.js, | 
					
						
							|  |  |  |     quickstart/js/src/index.html, | 
					
						
							|  |  |  |     quickstart/js/src/styles.1.css, | 
					
						
							|  |  |  |     quickstart/js/bs-config.1.json, | 
					
						
							|  |  |  |     quickstart/js/package.1.json | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   `,null, | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  |   `src/app/app.component.js, | 
					
						
							|  |  |  |   src/app/app.module.js, | 
					
						
							|  |  |  |   src/main.js, | 
					
						
							|  |  |  |   src/index.html, | 
					
						
							|  |  |  |   src/styles.css, | 
					
						
							|  |  |  |   bs-config.json, | 
					
						
							|  |  |  |   package.json | 
					
						
							|  |  |  |   `) | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .l-main-section | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   ## Wrap Up | 
					
						
							| 
									
										
										
										
											2016-09-20 05:24:40 +02:00
										 |  |  |   Our first application doesn't do much. It's basically "Hello, World" for Angular. | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   We kept it simple in our first pass: we wrote a little Angular component, | 
					
						
							|  |  |  |   we added some JavaScript libraries to `index.html`, and launched with a | 
					
						
							|  |  |  |   static file server. That's about all we'd expect to do for a "Hello, World" app. | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   **We have greater ambitions.** | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   The good news is that the overhead of setup is (mostly) behind us.  | 
					
						
							|  |  |  |   We'll probably only touch the `package.json` to update libraries. | 
					
						
							|  |  |  |   Besides adding in the script files for our app 'modules', | 
					
						
							|  |  |  |   we'll likely open `index.html` only if we need to add a library or some css stylesheets. | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   We're about to take the next step and build a small application that  | 
					
						
							| 
									
										
										
										
											2016-09-20 05:24:40 +02:00
										 |  |  |   demonstrates the great things we can build with Angular. | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   Join us on the [Tour of Heroes Tutorial](./tutorial)! | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | .l-main-section | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   ## Appendices | 
					
						
							|  |  |  |   The balance of this chapter is a set of appendices that | 
					
						
							|  |  |  |   elaborate some of the points we covered quickly above. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   There is no essential material here. Continued reading is for the curious. | 
					
						
							| 
									
										
										
										
											2016-01-17 22:30:19 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | <a id="ie-polyfills"></a> | 
					
						
							|  |  |  | <a id="es6support"></a> | 
					
						
							|  |  |  | <a id="libraries"></a> | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | .l-main-section | 
					
						
							|  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-17 22:30:19 -08:00
										 |  |  |   ### Appendix: Libraries | 
					
						
							|  |  |  |   We loaded the following scripts | 
					
						
							| 
									
										
										
										
											2017-02-02 18:38:17 +00:00
										 |  |  | +makeExample('quickstart/js/src/index.html', 'libraries', 'src/index.html')(format=".") | 
					
						
							| 
									
										
										
										
											2016-01-17 22:30:19 -08:00
										 |  |  | :marked | 
					
						
							|  |  |  |   We began with an Internet Explorer polyfill.  | 
					
						
							|  |  |  |   IE requires a polyfill to run | 
					
						
							|  |  |  |   an application that relies on ES2015 promises and dynamic module loading. | 
					
						
							|  |  |  |   Most applications need those capabilities and most applications | 
					
						
							|  |  |  |   should run in Internet Explorer. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 05:24:40 +02:00
										 |  |  |   Next are the polyfills for Angular, `zone.js` and `Reflect.js`, | 
					
						
							| 
									
										
										
										
											2016-01-17 22:30:19 -08:00
										 |  |  |   followed by the Reactive Extensions RxJS library. | 
					
						
							|  |  |  | .l-sub-section | 
					
						
							|  |  |  |   :marked | 
					
						
							|  |  |  |     Our QuickStart doesn't use the Reactive Extensions  | 
					
						
							|  |  |  |     but any substantial application will want them | 
					
						
							|  |  |  |     when working with observables. | 
					
						
							|  |  |  |     We added the library here in QuickStart so we don't forget later. | 
					
						
							|  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-09-20 05:24:40 +02:00
										 |  |  |   Finally, we loaded the web development version of Angular itself. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-17 22:30:19 -08:00
										 |  |  |   We'll make different choices as we gain experience and | 
					
						
							|  |  |  |   become more concerned about production qualities such as | 
					
						
							|  |  |  |   load times and memory footprint.   | 
					
						
							|  |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | .l-main-section | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   <a id="package-json"></a> | 
					
						
							|  |  |  |   ### Appendix: package.json | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   [npm](https://docs.npmjs.com/) is a popular package manager and Angular application developers rely on it | 
					
						
							|  |  |  |   to acquire and manage the libraries their apps require. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   We specify the packages we need in an npm [package.json](https://docs.npmjs.com/files/package.json) file. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   The Angular team suggests the packages listed in the `dependencies` and `devDependencies` | 
					
						
							|  |  |  |   sections listed in this file: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | +makeJson('quickstart/js/package.1.json',{ paths: 'dependencies, devDependencies'}, 'package.json (dependencies)')(format=".")  | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  | .l-sub-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |     There are other possible package choices.  | 
					
						
							|  |  |  |     We're recommending this particular set that we know work well together.  | 
					
						
							|  |  |  |     Play along with us for now.  | 
					
						
							|  |  |  |     Feel free to make substitutions later to suit your tastes and experience. | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   A `package.json` has an optional **scripts** section where we can define helpful | 
					
						
							|  |  |  |   commands to perform development and build tasks.  | 
					
						
							|  |  |  |   We've included a number of such scripts in our suggested `package.json`: | 
					
						
							|  |  |  | +makeJson('quickstart/js/package.1.json',{ paths: 'scripts'}, 'package.json (scripts)')(format=".")  | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   We've seen how we can run the server with this command: | 
					
						
							|  |  |  | code-example(format=""). | 
					
						
							|  |  |  |   npm start | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   We are using the special `npm start` command, but all it does is run `npm run lite`. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   We execute npm scripts in that manner: `npm run` + *script-name*. Here's what these scripts do: | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   * `npm run lite` - run the [lite-server](https://www.npmjs.com/package/lite-server),  | 
					
						
							|  |  |  |   a light-weight, static file server, written and maintained by [John Papa](http://johnpapa.net/)  | 
					
						
							|  |  |  |   with excellent support for Angular apps that use routing. | 
					
						
							| 
									
										
										
										
											2015-10-14 20:25:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | .l-main-section | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   <a id="npm-errors"></a> | 
					
						
							| 
									
										
										
										
											2016-09-21 13:32:36 -05:00
										 |  |  |   ### Appendix: npm errors and warnings | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2016-01-17 22:30:19 -08:00
										 |  |  |   All is well if there are no console messages starting with `npm ERR!` *at the end* of **npm install**.  | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   There might be a few `npm WARN` messages along the way — and that is perfectly fine. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   We often see an `npm WARN` message after a series of `gyp ERR!` messages.  | 
					
						
							|  |  |  |   Ignore them. A package may try to re-compile itself using `node-gyp`. | 
					
						
							|  |  |  |   If the re-compile fails, the package recovers (typically with a pre-built version) | 
					
						
							|  |  |  |   and everything works. | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-01-17 22:30:19 -08:00
										 |  |  |   Just make sure there are no `npm ERR!` messages at the very end of `npm install`. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  | .l-main-section | 
					
						
							|  |  |  | :marked | 
					
						
							|  |  |  |   <a id="ngmodule"></a> | 
					
						
							|  |  |  |   ### Appendix: ***NgModule*** | 
					
						
							|  |  |  |   The `NgModule` decorator is listing: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   1. What other Angular Modules ours uses | 
					
						
							|  |  |  |   1. Which components and directives we declare in our components | 
					
						
							|  |  |  |   1. The component to bootstrap at the start  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 18:39:49 +05:30
										 |  |  |   We import our lone `app.AppComponent` and add it to both `declarations` and `bootstrap` array. | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Notice that we also add `ng.platformBrowser.BrowserModule` to the `imports` array.  | 
					
						
							|  |  |  |   This is the Angular Module that contains all the needed Angular bits and pieces to run our app in the browser. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Angular itself is split into separate Angular Modules so we only need to import the ones we really use. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   One of the most common ones is `FormsModule`, and soon we'll also see `RouterModule` and `HttpModule`.  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | .l-main-section | 
					
						
							|  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-28 16:15:26 -08:00
										 |  |  |   <a id="main"></a> | 
					
						
							|  |  |  |   ### Appendix: ***main.js*** | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   #### Bootstrapping is platform-specific | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   We use the `platformBrowserDynamic().bootstrapModule` function from `ng.platformBrowserDynamic`, | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   not `ng.core`. There's a good reason. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   We only call "core" those capabilities that are the same across all platform targets. | 
					
						
							|  |  |  |   True, most Angular applications run only in a browser and we'll call the bootstrap function from | 
					
						
							|  |  |  |   this library most of the time. It's pretty "core" if we're always writing for a browser. | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2016-05-25 19:47:23 +05:30
										 |  |  |   But it is possible to load a component in a different environment.  | 
					
						
							| 
									
										
										
										
											2015-12-29 10:32:02 -05:00
										 |  |  |   We might load it on a mobile device with [Apache Cordova](https://cordova.apache.org/) or [NativeScript](https://www.nativescript.org/). | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   We might wish to render the first page of our application on the server  | 
					
						
							|  |  |  |   to improve launch performance or facilitate  | 
					
						
							| 
									
										
										
										
											2016-03-16 20:04:42 -07:00
										 |  |  |   [SEO](http://www.google.com/webmasters/docs/search-engine-optimization-starter-guide.pdf). | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   These targets require a different kind of bootstrap function that we'd import from a different library. | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   #### Why do we create a separate ***main.js***, ***app.module.js*** and ***app.component.js*** files? | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2016-01-28 16:15:26 -08:00
										 |  |  |   The *main.js* file is tiny. This is just a QuickStart. | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   We could have folded its few lines into the `app.module.js` file, and that one into | 
					
						
							|  |  |  |   `app.component.js` and spared ourselves some complexity. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   We didn't for what we believe to be good reasons: | 
					
						
							|  |  |  |   1. Doing it right is easy | 
					
						
							|  |  |  |   1. Testability | 
					
						
							|  |  |  |   1. Reusability | 
					
						
							|  |  |  |   1. Separation of concerns | 
					
						
							|  |  |  |   1. We learned about import and export | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   #### It's easy | 
					
						
							|  |  |  |   Sure it's an extra step and an extra file. How hard is that in the scheme of things? | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   We'll see that a separate `main.js` and `app.module.js` is beneficial for *most* apps  | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   even if it isn't critical for the QuickStart. | 
					
						
							|  |  |  |   Let's develop good habits now while the cost is low. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   #### Testability | 
					
						
							|  |  |  |   We should be thinking about testability from the beginning | 
					
						
							|  |  |  |   even if we know we'll never test the QuickStart.  | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   It is difficult to unit test a component when there is a call to `bootstrapModule` in the same file. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   As soon as we load the component file to test the component,  | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   the `bootstrapModule` function tries to load the application in the browser. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   It throws an error because we're not expecting to run the entire application,  | 
					
						
							|  |  |  |   just test the component.  | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   Relocating the `bootstrapModule` function to `main.js` eliminates this spurious error | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   and leaves us with a clean component module file. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   #### Reusability | 
					
						
							|  |  |  |   We refactor, rename, and relocate files as our application evolves. | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   We can't do any of those things while the file calls `bootstrapModule`. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   we can't move it.  | 
					
						
							|  |  |  |   We can't reuse the component in another application.  | 
					
						
							|  |  |  |   We can't pre-render the component on the server for better performance. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   #### Separation of concerns | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   A component's responsibility is to present and manage a view, and a NgModule's | 
					
						
							|  |  |  |   reponsibility is to define the application context. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   Launching the application has nothing to do with any of these. | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   That's a separate concern. The friction we're encountering in testing and reuse | 
					
						
							|  |  |  |   stems from this unnecessary mix of responsibilities. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   #### Import/Export | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   While writing a separate `main.js` and `app.module.js` files we learned an essential Angular skill: | 
					
						
							| 
									
										
										
										
											2015-12-15 11:38:42 +00:00
										 |  |  |   how to 'export' from one 'module' and 'import' into another via our simple | 
					
						
							|  |  |  |   namespace abstraction.  | 
					
						
							|  |  |  |   We'll do a lot of that as we learn more Angular. |