2016-05-14 20:40:52 -04:00
|
|
|
|
block includes
|
|
|
|
|
include _util-fns
|
|
|
|
|
- var _Install = 'Install'
|
|
|
|
|
- var _prereq = 'Node.js'
|
|
|
|
|
- var _angular_browser_uri = '@angular/platform-browser-dynamic'
|
|
|
|
|
- var _angular_core_uri = '@angular/core'
|
2015-10-14 23:25:19 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
Our QuickStart goal is to build and run a super-simple
|
|
|
|
|
Angular 2 application in #{_Lang}, and
|
2016-05-14 20:40:52 -04:00
|
|
|
|
establish a development environment for the remaining documentation samples
|
|
|
|
|
that also can be the foundation for real world applications.
|
|
|
|
|
|
|
|
|
|
.callout.is-helpful
|
|
|
|
|
header Don't want #{_Lang}?
|
|
|
|
|
p.
|
|
|
|
|
Although we're getting started in #{_Lang}, you can also write Angular 2 apps
|
|
|
|
|
in #{_docsFor == 'ts' ? 'Dart' : 'TypeScript'} and JavaScript.
|
|
|
|
|
Just select either of those languages from the combo-box in the banner.
|
|
|
|
|
|
2016-07-03 20:11:17 -04:00
|
|
|
|
:marked
|
|
|
|
|
# Try it!
|
|
|
|
|
|
|
|
|
|
Try the <live-example></live-example> which loads the sample app
|
|
|
|
|
<span if-docs="ts">
|
|
|
|
|
in <a href="http://plnkr.co/" title="Plunker" target="_blank">plunker</a>
|
|
|
|
|
</span>
|
|
|
|
|
and displays the simple message:
|
2015-10-14 23:25:19 -04:00
|
|
|
|
|
2015-12-10 21:29:14 -05:00
|
|
|
|
figure.image-display
|
2016-02-12 20:54:22 -05:00
|
|
|
|
img(src='/resources/images/devguide/quickstart/my-first-app.png' alt="Output of QuickStart app")
|
2016-03-13 15:50:50 -04:00
|
|
|
|
|
2015-12-10 21:29:14 -05:00
|
|
|
|
:marked
|
2016-07-03 20:11:17 -04:00
|
|
|
|
# Build this app!
|
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
- [Prerequisite](#prereq): Install #{_prereq}
|
|
|
|
|
- [Step 1](#create-and-configure): Create the app’s project folder and
|
2016-05-14 20:40:52 -04:00
|
|
|
|
define package dependencies and special project setup
|
2016-05-16 20:10:19 -04:00
|
|
|
|
- [Step 2](#root-component): Create the app’s Angular root component
|
2016-07-03 20:11:17 -04:00
|
|
|
|
- [Step 3](#main): Add <span ngio-ex>main.ts</span>, identifying the root component to Angular
|
2016-05-16 20:10:19 -04:00
|
|
|
|
- [Step 4](#index): Add `index.html`, the web page that hosts the application
|
|
|
|
|
- [Step 5](#build-and-run): Build and run the app
|
2016-05-14 20:40:52 -04:00
|
|
|
|
- [Make some changes to the app](#make-some-changes)
|
|
|
|
|
- [Wrap up](#wrap-up)
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
.l-main-section
|
|
|
|
|
h2#prereq Prerequisite: #{_prereq}
|
|
|
|
|
|
|
|
|
|
block setup-tooling
|
2015-12-10 21:29:14 -05:00
|
|
|
|
:marked
|
2016-05-14 20:40:52 -04:00
|
|
|
|
Install **[Node.js® and npm](https://nodejs.org/en/download/)**
|
|
|
|
|
if they are not already on your machine.
|
2016-05-21 12:36:27 -04:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2016-07-04 09:59:13 -04:00
|
|
|
|
**Verify that you are running at least node `v4.x.x` and npm `3.x.x`**
|
2016-05-21 12:36:27 -04:00
|
|
|
|
by running `node -v` and `npm -v` in a terminal/console window.
|
2016-05-28 16:49:13 -04:00
|
|
|
|
Older versions produce errors.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
|
|
|
|
|
block download-source
|
|
|
|
|
.l-main-section
|
|
|
|
|
.callout.is-helpful
|
|
|
|
|
header Download the source
|
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
Instead of following each step of these instructions, we can
|
|
|
|
|
[download the QuickStart source](https://github.com/angular/quickstart/blob/master/README.md)
|
2016-05-14 20:40:52 -04:00
|
|
|
|
from github and follow its brief instructions.
|
|
|
|
|
|
|
|
|
|
.l-main-section
|
2016-02-12 20:54:22 -05:00
|
|
|
|
button(class="verbose off md-primary md-button md-ink-ripple", type="button", onclick="verbose(false)").
|
|
|
|
|
Hide explanations
|
|
|
|
|
button(class="verbose on md-primary md-button md-ink-ripple", type="button", onclick="verbose(true)").
|
|
|
|
|
View explanations
|
|
|
|
|
.l-verbose-section
|
|
|
|
|
:marked
|
|
|
|
|
*Explanations* describe the concepts and reasons behind the instructions.
|
|
|
|
|
Explanations have a thin border on the left like *this* block of text.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
|
|
|
|
Click *Hide Explanations* to show only the instructions.
|
|
|
|
|
Click *View Explanations* to see everything again.
|
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
We'll see many code blocks as we build the QuickStart app. They're all easy to copy and paste:
|
|
|
|
|
code-example(format="nocode").
|
|
|
|
|
Click the glyph on the right to copy code snippets to the clipboard ==>
|
2015-10-14 23:25:19 -04:00
|
|
|
|
|
2015-10-16 00:51:13 -04:00
|
|
|
|
.l-main-section
|
2016-05-16 20:10:19 -04:00
|
|
|
|
h2#create-and-configure Step 1: Create and configure the project
|
2016-05-14 20:40:52 -04:00
|
|
|
|
|
|
|
|
|
- var _package_and_config_files = _docsFor == 'dart' ? 'pubspec.yaml' : 'package definition and configuration files'
|
|
|
|
|
|
2015-12-10 21:29:14 -05:00
|
|
|
|
:marked
|
2016-05-14 20:40:52 -04:00
|
|
|
|
In this step we:
|
2016-05-16 20:10:19 -04:00
|
|
|
|
* [(a) Create the project folder](#create-the-project-folder)
|
|
|
|
|
* [(b) Add #{_package_and_config_files}](#add-config-files)
|
|
|
|
|
* [(c) #{_Install} packages](#install-packages)
|
2016-05-14 20:40:52 -04:00
|
|
|
|
|
|
|
|
|
h3 (a) Create the project folder
|
|
|
|
|
|
2016-07-01 14:05:45 -04:00
|
|
|
|
- var _ = _docsFor == 'dart' ? '_' : '-';
|
2016-05-14 20:40:52 -04:00
|
|
|
|
code-example(language="sh").
|
2016-07-01 14:05:45 -04:00
|
|
|
|
mkdir angular2!{_}quickstart
|
|
|
|
|
cd angular2!{_}quickstart
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
h3#add-config-files (b) Add #{_package_and_config_files}
|
|
|
|
|
block package-and-config-files
|
|
|
|
|
- var _tsconfigUri = 'guide/typescript-configuration.html#tsconfig'
|
|
|
|
|
- var _typingsUri = 'guide/typescript-configuration.html#!#typings'
|
|
|
|
|
|
|
|
|
|
p Add the following package definition and configuration files to the project folder:
|
|
|
|
|
ul
|
|
|
|
|
li.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
#[b package.json] lists packages the QuickStart app depends on and
|
2016-05-14 20:40:52 -04:00
|
|
|
|
defines some useful scripts.
|
|
|
|
|
See #[a(href="guide/npm-packages.html") Npm Package Configuration] for details.
|
|
|
|
|
li.
|
|
|
|
|
#[b tsconfig.json] is the TypeScript compiler configuration file.
|
|
|
|
|
See #[a(href="#{_tsconfigUri}") TypeScript Configuration] for details.
|
|
|
|
|
li.
|
|
|
|
|
#[b typings.json] identifies TypeScript definition files.
|
|
|
|
|
See #[a(href="#{_typingsUri}") TypeScript Configuration] for details.
|
|
|
|
|
li.
|
|
|
|
|
#[b systemjs.config.js], the SystemJS configuration file.
|
|
|
|
|
See discussion #[a(href="#systemjs") below].
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
a#config-files
|
|
|
|
|
+makeTabs(`
|
|
|
|
|
quickstart/ts/package.1.json,
|
|
|
|
|
quickstart/ts/tsconfig.1.json,
|
|
|
|
|
quickstart/ts/typings.1.json,
|
|
|
|
|
quickstart/ts/systemjs.config.1.js
|
|
|
|
|
`, '', `
|
|
|
|
|
package.json,
|
|
|
|
|
tsconfig.json,
|
|
|
|
|
typings.json,
|
|
|
|
|
systemjs.config.js
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
|
|
h3#install-packages (c) #{_Install} packages
|
|
|
|
|
block install-packages
|
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
We install the packages listed in `package.json` using `npm`. Enter the
|
2016-05-14 20:40:52 -04:00
|
|
|
|
following command in a terminal window (command window in Windows):
|
2016-02-11 18:08:06 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
code-example(language="sh").
|
|
|
|
|
npm install
|
2016-04-13 13:20:51 -04:00
|
|
|
|
|
2016-06-15 11:55:14 -04:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
The `typings` folder could not show up after `npm install`. If so, please install them manually.
|
|
|
|
|
|
|
|
|
|
code-example(language="sh").
|
|
|
|
|
npm run typings install
|
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
.alert.is-important
|
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
Scary <span style="color:red; font-weight: bold">error messages in red</span> may appear **during** install.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
The install typically recovers from these errors and finishes successfully.
|
|
|
|
|
.l-verbose-section(class="l-verbose-inherit")
|
|
|
|
|
:marked
|
|
|
|
|
#### npm errors and warnings
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
All is well if there are no console messages starting with `npm ERR!` *at the end* of **npm install**.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
There might be a few `npm WARN` messages along the way — and that is perfectly fine.
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
We often see an `npm WARN` message after a series of `gyp ERR!` messages.
|
|
|
|
|
Ignore them. A package may try to recompile itself using `node-gyp`.
|
|
|
|
|
If the recompile fails, the package recovers (typically with a pre-built version)
|
2016-05-14 20:40:52 -04:00
|
|
|
|
and everything works.
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
Just make sure there are no `npm ERR!` messages at the end of `npm install`.
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
.l-verbose-section
|
|
|
|
|
:marked
|
|
|
|
|
#### Adding the libraries and packages we need with *npm*
|
|
|
|
|
Angular application developers rely on the _[npm](https://docs.npmjs.com)_
|
2016-05-16 20:10:19 -04:00
|
|
|
|
package manager to install the libraries and packages their apps require.
|
|
|
|
|
The Angular team recommends the starter-set of packages specified in the
|
2016-05-14 20:40:52 -04:00
|
|
|
|
`dependencies` and `devDependencies` sections.
|
|
|
|
|
See the [npm packages](guide/npm-packages.html) chapter for details.
|
|
|
|
|
|
|
|
|
|
#### Helpful scripts
|
|
|
|
|
We've included a number of npm scripts in our suggested `package.json` to handle common development tasks:
|
|
|
|
|
+makeJson('quickstart/ts/package.1.json',{ paths: 'scripts'}, 'package.json (scripts)')(format=".")
|
2016-02-17 22:31:42 -05:00
|
|
|
|
|
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
We execute most npm scripts in the following way: `npm run` followed by a *script-name*.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
Some commands (such as `start`) don't require the `run` keyword.
|
|
|
|
|
|
|
|
|
|
Here's what these scripts do:
|
|
|
|
|
|
|
|
|
|
* `npm start` - runs the compiler and a server at the same time, both in "watch mode"
|
|
|
|
|
|
|
|
|
|
* `npm run tsc` - runs the TypeScript compiler once
|
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
* `npm run tsc:w` - runs the TypeScript compiler in watch mode;
|
|
|
|
|
the process keeps running, awaiting changes to TypeScript files and recompiling when it sees them
|
2016-05-14 20:40:52 -04:00
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
* `npm run lite` - runs the <a href="https://www.npmjs.com/package/lite-server" target="_blank">lite-server</a>,
|
2016-05-14 20:40:52 -04:00
|
|
|
|
a light-weight, static file server with excellent support for Angular apps that use routing
|
|
|
|
|
|
|
|
|
|
* `npm run typings` - runs the [*typings* tool](#{_typingsUri}) separately
|
|
|
|
|
|
|
|
|
|
* `npm run postinstall` - called by *npm* automatically *after* it successfully completes package installation.
|
|
|
|
|
This script installs the [TypeScript definition files](#{_typingsUri}) defined in `typings.json`
|
2016-02-17 22:31:42 -05:00
|
|
|
|
|
2015-12-10 21:29:14 -05:00
|
|
|
|
:marked
|
|
|
|
|
**We're all set.** Let's write some code.
|
2015-08-08 16:55:53 -04:00
|
|
|
|
|
2015-10-14 23:25:19 -04:00
|
|
|
|
.l-main-section
|
2016-05-16 20:10:19 -04:00
|
|
|
|
h2#root-component Step 2: Our first Angular component
|
2015-12-10 21:29:14 -05:00
|
|
|
|
:marked
|
2016-02-12 20:54:22 -05:00
|
|
|
|
Let's create a folder to hold our application and add a super-simple Angular component.
|
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
**Create #{_an} #{_appDir} subfolder** off the project root directory:
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
code-example.
|
|
|
|
|
mkdir #{_appDir}
|
|
|
|
|
|
|
|
|
|
a#app-component
|
|
|
|
|
p.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
#[b Create the component file]
|
2016-05-14 20:40:52 -04:00
|
|
|
|
#[code #[+adjExPath('app/app.component.ts')]] (in this newly created directory) with the following content:
|
|
|
|
|
|
2016-06-02 12:06:03 -04:00
|
|
|
|
+makeExample('app/app.component.ts')
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
.l-verbose-section
|
|
|
|
|
:marked
|
|
|
|
|
### AppComponent is the root of the application
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
Every Angular app has at least one **root component**, conventionally named `AppComponent`,
|
2016-02-12 20:54:22 -05:00
|
|
|
|
that hosts the client user experience.
|
|
|
|
|
Components are the basic building blocks of Angular applications.
|
|
|
|
|
A component controls a portion of the screen — a *view* — through its associated template.
|
2015-12-14 18:44:50 -05:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
This QuickStart has only one, extremely simple component.
|
|
|
|
|
But it has the essential structure of every component we'll ever write:
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
* One or more [import](#component-import)
|
2016-05-16 20:10:19 -04:00
|
|
|
|
statements to reference the things we need.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
* A [@Component #{_decorator}](#component-decorator)
|
2016-02-12 20:54:22 -05:00
|
|
|
|
that tells Angular what template to use and how to create the component.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
* A [component class](#component-class)
|
2016-02-12 20:54:22 -05:00
|
|
|
|
that controls the appearance and behavior of a view through its template.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
a#component-import
|
2016-02-12 20:54:22 -05:00
|
|
|
|
:marked
|
|
|
|
|
### Import
|
2015-10-14 23:25:19 -04:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
Angular apps are modular. They consist of many files each dedicated to a purpose.
|
|
|
|
|
Angular itself is modular. It is a collection of library modules
|
|
|
|
|
each made up of several, related features that we'll use to build our application.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
|
|
|
|
When we need something from a module or library, we import it.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
Here we import the Angular 2 core so that our component code can have access to
|
|
|
|
|
the `@Component` #{_decorator}.
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-18 13:27:51 -04:00
|
|
|
|
+makeExcerpt('app/app.component.ts', 'import')
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
h3#component-decorator @Component #{_decorator}
|
|
|
|
|
+ifDocsFor('ts')
|
|
|
|
|
:marked
|
|
|
|
|
`Component` is a *decorator function* that takes a *metadata object* as argument.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
We apply this function to the component class by prefixing the function with the
|
2016-05-14 20:40:52 -04:00
|
|
|
|
**@** symbol and invoking it with a metadata object, just above the class.
|
2016-02-12 20:54:22 -05:00
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
`@Component` is #{_a} *#{_decorator}* that allows us to associate *metadata* with the
|
2016-05-14 20:40:52 -04:00
|
|
|
|
component class.
|
|
|
|
|
The metadata tells Angular how to create and use this component.
|
|
|
|
|
|
2016-05-18 13:27:51 -04:00
|
|
|
|
+makeExcerpt('app/app.component.ts', 'metadata')
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
block annotation-fields
|
|
|
|
|
:marked
|
|
|
|
|
This particular metadata object has two fields, a `selector` and a `template`.
|
|
|
|
|
:marked
|
2016-02-12 20:54:22 -05:00
|
|
|
|
The **selector** specifies a simple CSS selector for an HTML element that represents the component.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
>The element for this component is named `my-app`.
|
|
|
|
|
Angular creates and displays an instance of our `AppComponent`
|
|
|
|
|
wherever it encounters a `my-app` element in the host HTML.
|
2015-12-10 21:29:14 -05:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
The **template** specifies the component's companion template,
|
|
|
|
|
written in an enhanced form of HTML that tells Angular how to render this component's view.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-05-17 02:16:11 -04:00
|
|
|
|
>Our template is a single line of HTML announcing "*My First Angular 2 App*".
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
>A more advanced template could contain data bindings to component properties
|
|
|
|
|
and might identify other application components which have their own templates.
|
|
|
|
|
These templates might identify yet other components.
|
|
|
|
|
In this way an Angular application becomes a tree of components.
|
|
|
|
|
|
|
|
|
|
:marked
|
|
|
|
|
### Component class
|
|
|
|
|
At the bottom of the file is an empty, do-nothing class named `AppComponent`.
|
2016-05-18 13:27:51 -04:00
|
|
|
|
+makeExcerpt('app/app.component.ts', 'class')
|
2016-02-12 20:54:22 -05:00
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
When we're ready to build a substantive application,
|
|
|
|
|
we can expand this class with properties and application logic.
|
2016-02-12 20:54:22 -05:00
|
|
|
|
Our `AppComponent` class is empty because we don't need it to do anything in this QuickStart.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
+ifDocsFor('ts')
|
|
|
|
|
:marked
|
|
|
|
|
We **export** `AppComponent` so that we can **import** it elsewhere in our application,
|
|
|
|
|
as we'll see when we create `main.ts`.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
.l-main-section
|
2016-05-16 20:10:19 -04:00
|
|
|
|
h2#main Step 3: Add #[code #[+adjExPath('main.ts')]]
|
2016-05-14 20:40:52 -04:00
|
|
|
|
|
|
|
|
|
block create-main
|
|
|
|
|
p.
|
|
|
|
|
Now we need something to tell Angular to load the root component.
|
|
|
|
|
Create the file #[code #[+adjExPath('app/main.ts')]] with the following content:
|
|
|
|
|
|
2016-06-02 12:06:03 -04:00
|
|
|
|
+makeExample('app/main.ts')
|
2016-02-10 17:58:05 -05:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
.l-verbose-section
|
2015-12-10 21:29:14 -05:00
|
|
|
|
:marked
|
2016-02-12 20:54:22 -05:00
|
|
|
|
We import the two things we need to launch the application:
|
2015-08-08 16:55:53 -04:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
1. Angular's browser `bootstrap` function
|
|
|
|
|
1. The application root component, `AppComponent`.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
Then we call `bootstrap` with `AppComponent`.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
### Bootstrapping is platform-specific
|
2016-05-14 20:40:52 -04:00
|
|
|
|
Notice that we import the `bootstrap` function from `#{_angular_browser_uri}`,
|
|
|
|
|
not `#{_angular_core_uri}`.
|
2016-02-12 20:54:22 -05:00
|
|
|
|
Bootstrapping isn't core because there isn't a single way to bootstrap the app.
|
|
|
|
|
True, most applications that run in a browser call the bootstrap function from
|
|
|
|
|
this library.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
|
|
|
|
But it is possible to load a component in a different environment.
|
2016-02-12 20:54:22 -05:00
|
|
|
|
We might load it on a mobile device with [Apache Cordova](https://cordova.apache.org/) or [NativeScript](https://www.nativescript.org/).
|
|
|
|
|
We might wish to render the first page of our application on the server
|
2016-05-16 20:10:19 -04:00
|
|
|
|
to improve launch performance or facilitate
|
2016-03-16 23:04:42 -04:00
|
|
|
|
[SEO](http://www.google.com/webmasters/docs/search-engine-optimization-starter-guide.pdf).
|
2016-02-12 20:54:22 -05:00
|
|
|
|
These targets require a different kind of bootstrap function that we'd import from a different library.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-07-03 20:11:17 -04:00
|
|
|
|
### Why create separate *<span ngio-ex>main.ts</span>* and app component files?
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-07-03 20:11:17 -04:00
|
|
|
|
Both <span ngio-ex>main.ts</span> and the app component files are tiny.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
This is just a QuickStart.
|
|
|
|
|
We could have merged these two files into one
|
2016-02-12 20:54:22 -05:00
|
|
|
|
and spared ourselves some complexity.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
We'd rather demonstrate the proper way to structure an Angular application.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
App bootstrapping is a separate concern from presenting a view.
|
2016-02-12 20:54:22 -05:00
|
|
|
|
Mixing concerns creates difficulties down the road.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
We might launch the `AppComponent` in multiple environments with different bootstrappers.
|
2016-02-12 20:54:22 -05:00
|
|
|
|
Testing the component is much easier if it doesn't also try to run the entire application.
|
|
|
|
|
Let's make the small extra effort to do it *the right way*.
|
|
|
|
|
|
2015-08-08 16:55:53 -04:00
|
|
|
|
.l-main-section
|
2016-05-16 20:10:19 -04:00
|
|
|
|
h2#index Step 4: Add #[code index.html]
|
2015-12-10 21:29:14 -05:00
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
In the *#{_indexHtmlDir}* folder
|
|
|
|
|
create an `index.html` file and paste the following lines into it:
|
2015-08-08 16:55:53 -04:00
|
|
|
|
|
2016-06-02 12:06:03 -04:00
|
|
|
|
+makeExample('index.html')
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
.l-verbose-section
|
2016-05-16 20:10:19 -04:00
|
|
|
|
:marked
|
|
|
|
|
The `index.html` file defines the web page that hosts the application.
|
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
block index-html-commentary-for-ts
|
|
|
|
|
:marked
|
|
|
|
|
The noteworthy sections of HTML are:
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
1. The JavaScript [libraries](#libraries)
|
|
|
|
|
2. Configuration file for [SystemJS](#systemjs), and a script
|
|
|
|
|
where we import and run the `app` module which refers to the `main` file that we just wrote.
|
|
|
|
|
3. The [`<my-app>`](#my-app) tag in the `<body>` which is *where our app lives!*
|
2016-05-05 17:26:31 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
:marked
|
|
|
|
|
### Libraries
|
|
|
|
|
We loaded the following scripts
|
2016-05-18 13:27:51 -04:00
|
|
|
|
+makeExcerpt('index.html', 'libraries')
|
2016-05-14 20:40:52 -04:00
|
|
|
|
:marked
|
2016-05-20 02:18:18 -04:00
|
|
|
|
We begin with `core-js`'s ES2015/ES6 shim which monkey patches the global context (window) with essential features of ES2015 (ES6).
|
2016-05-14 20:40:52 -04:00
|
|
|
|
Next are the polyfills for Angular2, `zone.js` and `reflect-metadata`.
|
|
|
|
|
Then the [SystemJS](#systemjs) library for module loading.
|
2016-02-12 20:54:22 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
We'll make different choices as we gain experience and
|
|
|
|
|
become more concerned about production qualities such as
|
|
|
|
|
load times and memory footprint.
|
2016-05-05 17:26:31 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
h3#systemjs SystemJS
|
2016-05-03 11:43:03 -04:00
|
|
|
|
:marked
|
2016-05-14 20:40:52 -04:00
|
|
|
|
QuickStart uses <a href="https://github.com/systemjs/systemjs" target="_blank">SystemJS</a>
|
|
|
|
|
to load application and library modules. [Earlier](#add-config-files) we
|
|
|
|
|
added the `systemjs.config.js` file to the project root.
|
|
|
|
|
There are alternatives that work just fine including the well-regarded
|
|
|
|
|
[webpack](guide/webpack.html).
|
2016-05-16 20:10:19 -04:00
|
|
|
|
SystemJS happens to be a good choice.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
But we want to be clear that it was a *choice* and not a *preference*.
|
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
All module loaders require configuration and all loader configuration
|
2016-05-14 20:40:52 -04:00
|
|
|
|
becomes complicated rather quickly as soon as the file structure diversifies and
|
|
|
|
|
we start thinking about building for production and performance.
|
|
|
|
|
|
|
|
|
|
We suggest becoming well-versed in the loader of your choice.
|
|
|
|
|
Learn more about SystemJS configuration
|
|
|
|
|
<a href="https://github.com/systemjs/systemjs/blob/master/docs/config-api.md" target="_blank">here</a>.
|
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
With those cautions in mind, what are we doing in the
|
2016-05-14 20:40:52 -04:00
|
|
|
|
QuickStart [`systemjs.config.js` configuration file we added earlier](#config-files)?
|
|
|
|
|
First, we create a map to tell SystemJS where to look when we import some module.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
Then, we register all our packages to SystemJS:
|
2016-05-14 20:40:52 -04:00
|
|
|
|
all the project dependencies and our application package, `app`.
|
|
|
|
|
|
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
Our QuickStart doesn't use all of the listed packages
|
2016-05-14 20:40:52 -04:00
|
|
|
|
but any substantial application will want many of them
|
|
|
|
|
and all of the listed packages are required by at least one of the documentation samples.
|
|
|
|
|
|
|
|
|
|
There is no runtime harm in listing packages that we don't need as they will only be loaded when requested.
|
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
The `app` package tells SystemJS what to do when it sees a request for a
|
|
|
|
|
module from the `app/` folder.
|
2016-05-05 17:26:31 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
Our QuickStart makes such requests when one of its
|
|
|
|
|
application TypeScript files has an import statement like this:
|
2016-05-18 13:27:51 -04:00
|
|
|
|
+makeExcerpt('app/main.ts', 'import')
|
2016-02-12 20:54:22 -05:00
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
Notice that the module name (after `from`) does not mention a filename extension.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
In the configuration we tell SystemJS to default the extension to `js`, a JavaScript file.
|
|
|
|
|
|
|
|
|
|
That makes sense because we transpile TypeScript to JavaScript
|
|
|
|
|
*before* running the application.
|
|
|
|
|
|
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
#### Transpiling in the browser
|
|
|
|
|
In the live example on plunker we transpile (AKA compile) to JavaScript in the browser
|
2016-05-16 20:10:19 -04:00
|
|
|
|
on the fly. _That's fine for a demo_.
|
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
**Do not transpile in the browser during development or for production**.
|
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
We strongly recommend transpiling (AKA compiling) to JavaScript during a build phase
|
2016-05-14 20:40:52 -04:00
|
|
|
|
before running the application for several reasons including:
|
|
|
|
|
|
|
|
|
|
* We see compiler warnings and errors that are hidden from us in the browser.
|
|
|
|
|
|
2016-05-25 07:17:02 -04:00
|
|
|
|
* Precompilation simplifies the module loading process and
|
2016-05-14 20:40:52 -04:00
|
|
|
|
it's much easier to diagnose problems when this is a separate, external step.
|
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
* Precompilation means a faster user experience because the browser doesn't waste time compiling.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
* We iterate development faster because we only recompile changed files.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
We notice the difference as soon as the app grows beyond a handful of files.
|
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
* Precompilation fits into a continuous integration process of build, test, deploy.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
|
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
The `System.import` call tells SystemJS to import the `main` file
|
2016-05-14 20:40:52 -04:00
|
|
|
|
(`main.js` ... after transpiling `main.ts`, remember?);
|
|
|
|
|
`main` is where we tell Angular to launch the application.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
We also catch and log launch errors to the console.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
|
|
|
|
|
All other modules are loaded upon request
|
|
|
|
|
either by an import statement or by Angular itself.
|
|
|
|
|
|
2016-05-16 20:10:19 -04:00
|
|
|
|
### *<my-app>*
|
|
|
|
|
|
2016-02-12 20:54:22 -05:00
|
|
|
|
a(id="my-app")
|
|
|
|
|
:marked
|
2016-07-03 20:11:17 -04:00
|
|
|
|
When Angular calls the `bootstrap` function in <span ngio-ex>main.ts</span>, it reads the `AppComponent`
|
2016-02-12 20:54:22 -05:00
|
|
|
|
metadata, finds the `my-app` selector, locates an element tag named `my-app`,
|
2016-05-14 20:40:52 -04:00
|
|
|
|
and renders our application's view between those tags.
|
2015-08-08 16:55:53 -04:00
|
|
|
|
|
2016-03-13 15:50:50 -04:00
|
|
|
|
:marked
|
2016-05-14 20:40:52 -04:00
|
|
|
|
### Add some style
|
2016-05-16 20:10:19 -04:00
|
|
|
|
Styles aren't essential but they're nice, and `index.html` assumes we have
|
2016-03-13 15:50:50 -04:00
|
|
|
|
a stylesheet called `styles.css`.
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
see [styles.css](https://github.com/angular/angular.io/blob/master/public/docs/_examples/styles.css).
|
2016-05-18 13:27:51 -04:00
|
|
|
|
+makeExcerpt('styles.1.css')
|
2016-05-14 20:40:52 -04:00
|
|
|
|
|
2015-10-14 23:25:19 -04:00
|
|
|
|
.l-main-section
|
2016-05-16 20:10:19 -04:00
|
|
|
|
h2#build-and-run Step 5: Build and run the app!
|
2016-05-14 20:40:52 -04:00
|
|
|
|
block run-app
|
|
|
|
|
:marked
|
|
|
|
|
Open a terminal window and enter this command:
|
|
|
|
|
code-example.
|
|
|
|
|
npm start
|
|
|
|
|
:marked
|
|
|
|
|
That command runs two parallel node processes
|
|
|
|
|
1. The TypeScript compiler in watch mode
|
|
|
|
|
1. A static server called **lite-server** that loads `index.html` in a browser
|
|
|
|
|
and refreshes the browser when application files change
|
2015-10-14 23:25:19 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
In a few moments, a browser tab should open and display
|
2015-10-14 23:25:19 -04:00
|
|
|
|
|
2015-12-10 21:29:14 -05:00
|
|
|
|
figure.image-display
|
2016-05-16 20:10:19 -04:00
|
|
|
|
img(src='/resources/images/devguide/quickstart/my-first-app.png' alt="Output of QuickStart app")
|
2015-10-14 23:25:19 -04:00
|
|
|
|
|
2015-12-10 21:29:14 -05:00
|
|
|
|
:marked
|
2016-05-14 20:40:52 -04:00
|
|
|
|
**Great job!**
|
2015-08-08 16:55:53 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
block build-app
|
|
|
|
|
//- Nothing for ts.
|
|
|
|
|
|
|
|
|
|
:marked
|
|
|
|
|
## Make some changes
|
2016-05-16 20:10:19 -04:00
|
|
|
|
|
2015-12-10 21:29:14 -05:00
|
|
|
|
Try changing the message to "My SECOND Angular 2 app".
|
2016-05-16 20:10:19 -04:00
|
|
|
|
block server-watching
|
2016-05-14 20:40:52 -04:00
|
|
|
|
:marked
|
|
|
|
|
The TypeScript compiler and `lite-server` are watching.
|
|
|
|
|
They should detect the change, recompile the TypeScript into JavaScript,
|
|
|
|
|
refresh the browser, and display the revised message.
|
|
|
|
|
It's a nifty way to develop an application!
|
2015-12-10 21:29:14 -05:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
We close the terminal window when we're done to terminate both the compiler and the server.
|
2015-08-08 16:55:53 -04:00
|
|
|
|
|
2015-10-14 23:25:19 -04:00
|
|
|
|
.l-main-section
|
2015-12-10 21:29:14 -05:00
|
|
|
|
:marked
|
2016-05-14 20:40:52 -04:00
|
|
|
|
# Wrap up
|
|
|
|
|
|
2015-12-10 21:29:14 -05:00
|
|
|
|
Our final project folder structure looks like this:
|
2016-05-14 20:40:52 -04:00
|
|
|
|
block project-file-structure
|
|
|
|
|
.filetree
|
|
|
|
|
.file angular2-quickstart
|
2015-12-15 17:45:45 -05:00
|
|
|
|
.children
|
2016-05-14 20:40:52 -04:00
|
|
|
|
.file app
|
|
|
|
|
.children
|
|
|
|
|
.file app.component.ts
|
|
|
|
|
.file main.ts
|
|
|
|
|
.file node_modules ...
|
|
|
|
|
.file typings ...
|
|
|
|
|
.file index.html
|
|
|
|
|
.file package.json
|
|
|
|
|
.file styles.css
|
|
|
|
|
.file systemjs.config.js
|
|
|
|
|
.file tsconfig.json
|
|
|
|
|
.file typings.json
|
2015-12-10 21:29:14 -05:00
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
Here are the file contents:
|
2016-05-05 17:26:31 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
block project-files
|
|
|
|
|
+makeTabs(`
|
|
|
|
|
quickstart/ts/app/app.component.ts,
|
|
|
|
|
quickstart/ts/app/main.ts,
|
|
|
|
|
quickstart/ts/index.html,
|
|
|
|
|
quickstart/ts/package.1.json,
|
|
|
|
|
quickstart/ts/tsconfig.1.json,
|
|
|
|
|
quickstart/ts/typings.1.json,
|
|
|
|
|
quickstart/ts/styles.1.css,
|
|
|
|
|
quickstart/ts/systemjs.config.1.js`
|
|
|
|
|
,null,
|
2016-05-16 20:10:19 -04:00
|
|
|
|
`app/app.component.ts,
|
|
|
|
|
app/main.ts,
|
2016-05-14 20:40:52 -04:00
|
|
|
|
index.html,
|
2016-05-16 20:10:19 -04:00
|
|
|
|
package.json,
|
|
|
|
|
tsconfig.json,
|
2016-05-14 20:40:52 -04:00
|
|
|
|
typings.json,
|
|
|
|
|
styles.css,
|
|
|
|
|
systemjs.config.js`)
|
2015-08-08 16:55:53 -04:00
|
|
|
|
|
|
|
|
|
.l-main-section
|
2015-12-10 21:29:14 -05:00
|
|
|
|
:marked
|
2016-05-14 20:40:52 -04:00
|
|
|
|
## What next?
|
2015-12-10 21:29:14 -05:00
|
|
|
|
Our first application doesn't do much. It's basically "Hello, World" for Angular 2.
|
2015-10-14 23:25:19 -04:00
|
|
|
|
|
2015-12-10 21:29:14 -05:00
|
|
|
|
We kept it simple in our first pass: we wrote a little Angular component,
|
2016-05-14 20:40:52 -04:00
|
|
|
|
created a simple `index.html`, and launched with a
|
2015-12-10 21:29:14 -05:00
|
|
|
|
static file server. That's about all we'd expect to do for a "Hello, World" app.
|
2015-08-08 16:55:53 -04:00
|
|
|
|
|
2016-05-14 20:40:52 -04:00
|
|
|
|
**We have greater ambitions!**
|
|
|
|
|
block what-next-ts-overhead
|
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
The good news is that the overhead of setup is (mostly) behind us.
|
2016-05-14 20:40:52 -04:00
|
|
|
|
We'll probably only touch the `package.json` to update libraries.
|
|
|
|
|
We'll likely open `index.html` only if we need to add a library or some css stylesheets.
|
|
|
|
|
:marked
|
2016-05-16 20:10:19 -04:00
|
|
|
|
We're about to take the next step and build a small application that
|
2015-12-10 21:29:14 -05:00
|
|
|
|
demonstrates the great things we can build with Angular 2.
|
2015-08-08 16:55:53 -04:00
|
|
|
|
|
2015-12-10 21:29:14 -05:00
|
|
|
|
Join us on the [Tour of Heroes Tutorial](./tutorial)!
|