2016-05-14 20:40:52 -04:00
block includes
include _util-fns
- var _Install = 'Install'
2016-07-27 03:24:54 -04:00
- var _prereq = 'Node.js and npm'
2016-05-14 20:40:52 -04:00
- var _angular_browser_uri = '@angular/platform-browser-dynamic'
- var _angular_core_uri = '@angular/core'
2016-07-27 03:24:54 -04:00
- var _stepInit = 3 // Step # after NgModule step
2016-08-10 16:32:07 -04:00
- var _quickstartSrcURL='https://github.com/angular/quickstart/blob/master/README.md'
//- TS/Dart shared step counter
- var step = _stepInit
2015-10-14 23:25:19 -04:00
2016-05-14 20:40:52 -04:00
:marked
2016-07-27 03:24:54 -04:00
This QuickStart guide demonstrates how to build and run a simple Angular 2 application in #{_Lang}.
It also establishes a common development environment used throughout the code examples in these
guides.
2016-05-14 20:40:52 -04:00
.callout.is-helpful
header Don't want #{_Lang}?
p.
2016-07-27 03:24:54 -04:00
Although you're getting started in #{_Lang}, you can also write Angular 2 applications
in JavaScript and #{_docsFor == 'ts' ? 'Dart' : 'TypeScript'}. Use the language selector in the
banner to switch development languages for this guide.
.l-main-section
h1 Overview
2016-05-14 20:40:52 -04:00
2016-07-27 03:24:54 -04:00
aside.is-right
:marked
The live example link opens the finished application <span if-docs="ts"> in
<a href="http://plnkr.co/" title="Plunker" target="_blank">Plunker</a> so that you can interact
with the code. You'll find live examples at the start of most sections.
2016-07-03 20:11:17 -04:00
2016-07-27 03:24:54 -04:00
:marked
The QuickStart application has the structure of a real-world Angular application 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-27 03:24:54 -04:00
**Try it out**. Here's a link to a <live-example></live-example>.
2016-09-03 15:15:03 -04:00
You can also <a href="https://github.com/angular/quickstart/blob/master/README.md" target="_blank">
2016-07-27 03:24:54 -04:00
clone the entire QuickStart application</a> from GitHub.
h2 Build this application!
:marked
- [Prerequisite](#prereq): Install #{_prereq}.
- [Step 1](#create-and-configure): Create and configure the project.
<li if-docs="ts">[Step 2](#ngmodule): Create your application.</li>
- [Step !{step++}](#root-component): Create a component and add it to your application.
- [Step !{step++}](#main): Start up your application.
- [Step !{step++}](#index): Define the web page that hosts the application.
- [Step !{step++}](#build-and-run): Build and run the application.
- [Step !{step++}](#make-changes): Make some live changes.
- [Wrap up and Next Steps](#wrap-up)
2016-02-12 20:54:22 -05:00
2016-08-10 16:32:07 -04:00
- var step = _stepInit // reinitialize step counter for headers to come
2016-07-27 03:24:54 -04:00
2016-08-10 16:32:07 -04:00
.l-main-section#prereq
2016-07-27 03:24:54 -04:00
h1 Prerequisite: Install #{_prereq}
2016-05-14 20:40:52 -04:00
block setup-tooling
2015-12-10 21:29:14 -05:00
:marked
2016-07-27 03:24:54 -04:00
If Node.js and npm aren't already on your machine, <a href="http://blog.npmjs.org/post/85484771375/how-to-install-npm"
target="_blank">install them</a>. Our examples require node **v5.x.x** or higher and
npm **3.x.x** or higher. To check which version you are using, run `node -v` and `npm -v`
in a terminal window.
2016-05-14 20:40:52 -04:00
2016-07-27 03:24:54 -04:00
aside.is-right
2016-08-10 16:32:07 -04:00
:marked
2016-07-27 03:24:54 -04:00
To easily copy text from a code example, click the *Copy to Clipboard* icon (<i
class="material-icons" style="font-size: 100%">content_copy </i>) in the upper right corner of
the example box.
2016-05-14 20:40:52 -04:00
.l-main-section
2016-07-27 03:24:54 -04:00
h1#create-and-configure Step 1: Create and configure the project
2016-05-16 20:10:19 -04:00
2016-07-27 03:24:54 -04:00
- var _package_and_config_files = _docsFor == 'dart' ? 'pubspec.yaml' : 'configuration files'
2015-10-14 23:25:19 -04:00
2015-12-10 21:29:14 -05:00
:marked
2016-07-27 03:24:54 -04:00
In this step you will:
* [Create the project folder](#create-the-project-folder)
* [Create #{_package_and_config_files}](#add-config-files)
* [#{_Install} packages](#install-packages)
2016-05-14 20:40:52 -04:00
2016-07-27 03:24:54 -04:00
h2 Create the project folder
:marked
Using a terminal window, create a directory for the project, and change into this
directory.
2016-05-14 20:40:52 -04:00
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-07-27 03:24:54 -04:00
h2#add-config-files Create #{_package_and_config_files}
2016-05-14 20:40:52 -04:00
block package-and-config-files
- var _tsconfigUri = 'guide/typescript-configuration.html#tsconfig'
2016-07-27 03:24:54 -04:00
p Our typical Angular project needs several configuration files:
2016-05-14 20:40:52 -04:00
ul
li.
2016-07-27 03:24:54 -04:00
#[b package.json] identifies npm package dependencies for the project.
2016-05-14 20:40:52 -04:00
li.
2016-07-27 03:24:54 -04:00
#[b tsconfig.json] defines how the TypeScript compiler generates JavaScript from the project's
files.
2016-05-14 20:40:52 -04:00
li.
2016-07-27 03:24:54 -04:00
#[b typings.json] provides additional definition files for libraries that the TypeScript
compiler doesn't natively recognize.
2016-05-14 20:40:52 -04:00
li.
2016-07-27 03:24:54 -04:00
#[b systemjs.config.js] provides information to a module loader about where to find
application modules, and registers all the necessary packages. It also
contains other packages that will be needed by later documentation examples.
p.
Create each of these files in your project directory. Populate them by pasting in text from
the tabs in the example box 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
`)
2016-07-27 03:24:54 -04:00
p.
Learn more about these configuration files in the
#[a(href="guide/npm-packages.html") Npm Package Configuration] guide and the
#[a(href="#{_tsconfigUri}") TypeScript Configuration] guide.
A detailed discussion of module loading is beyond the scope of this guide.
.callout.is-helpful
header SystemJS or Webpack?
p.
Although we use SystemJS for illustrative purposes here, it's only one option for loading
modules. Use the module loader that you prefer. For Webpack and Angular, see <a
href="guide/webpack.html" >
Webpack: an Introduction</a>. Or, learn more about SystemJS configuration in general <a href="https://github.com/systemjs/systemjs/blob/master/docs/config-api.md" target="_blank">here</a>.
h2#install-packages #{_Install} packages
2016-05-14 20:40:52 -04:00
block install-packages
:marked
2016-07-27 03:24:54 -04:00
Using npm from the command line, install the packages listed in `package.json` with the command:
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-07-27 03:24:54 -04:00
:marked
Error messages—in red—might appear during the install, and you might see `npm WARN` messages. As long as there are no `npm ERR!` messages at the end, you can assume success.
2016-02-12 20:54:22 -05:00
2016-07-27 03:24:54 -04:00
:marked
You should now have the following structure:
2016-02-12 20:54:22 -05:00
2016-07-27 03:24:54 -04:00
.filetree
.file angular2-quickstart
.children
.file node_modules ...
.file typings ...
.file package.json
.file systemjs.config.js
.file tsconfig.json
.file typings.json
2016-02-12 20:54:22 -05:00
2016-07-27 03:24:54 -04:00
:marked
If the `typings` folder doesn't show up after running `npm install`, you'll need to install it manually with the command:
2016-05-14 20:40:52 -04:00
2016-07-27 03:24:54 -04:00
code-example(language="sh").
npm run typings install
2016-02-17 22:31:42 -05:00
2016-07-27 03:24:54 -04:00
:marked
You're now ready to write some code!
+ifDocsFor('ts')
.l-main-section
h1#ngmodule Step 2: Create your application
:marked
You compose Angular applications into closely related blocks of functionality with
[NgModules](guide/ngmodule.html). Angular itself is split into separate Angular Modules. This
makes it possible for you to keep payload size small by only importing the parts of Angular
that your application needs.
2016-05-14 20:40:52 -04:00
2016-07-27 03:24:54 -04:00
Every Angular application has at least one module: the _root module_, named `AppModule` here.
2016-05-14 20:40:52 -04:00
2016-07-27 03:24:54 -04:00
Create the file `app/app.module.ts` with the following content:
2016-05-14 20:40:52 -04:00
2016-09-01 11:24:15 -04:00
+makeExample('app/app.module.1.ts')(format='.')
2016-05-14 20:40:52 -04:00
2016-07-27 03:24:54 -04:00
:marked
This is the entry point to your application.
2016-05-14 20:40:52 -04:00
2016-07-27 03:24:54 -04:00
Since the QuickStart application is a web application that runs in a browser, your root module
needs to import the
[`BrowserModule`](../latest/api/platform-browser/index/BrowserModule-class.html)
from `@angular/platform-browser` to the `imports` array.
2016-05-14 20:40:52 -04:00
2016-07-27 03:24:54 -04:00
This is the smallest amount of Angular that is needed for a minimal application to run in the
browser.
2016-05-14 20:40:52 -04:00
2016-07-27 03:24:54 -04:00
The QuickStart application doesn't do anything else, so you don't need any other modules. In a real
application, you'd likely import [`FormsModule`](../latest/api/forms/index/FormsModule-class
.html) as well as [`RouterModule`](../latest/api/router/index/RouterModule-class.html) and
[`HttpModule`](../latest/api/http/index/HttpModule-class.html). These are introduced in the
[Tour of Heroes Tutorial](../tutorial/).
2016-02-17 22:31:42 -05:00
2015-08-08 16:55:53 -04:00
2016-07-27 03:24:54 -04:00
.l-main-section
h1#root-component Step !{step++}: Create a component and add it to your application
2015-12-10 21:29:14 -05:00
:marked
2016-07-27 03:24:54 -04:00
Every Angular application has at least one component: the _root component_, named `AppComponent`
here.
2016-08-10 16:32:07 -04:00
2016-07-27 03:24:54 -04:00
Components are the basic building blocks of Angular applications. A component controls a portion
of the screen—a *view*—through its associated template
2016-02-12 20:54:22 -05:00
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-07-27 03:24:54 -04:00
:marked
The QuickStart application has the same essential structure as any other Angular component:
2016-08-09 12:38:25 -04:00
2016-07-27 03:24:54 -04:00
* **An import statement**. Importing gives your component access to
Angular's core [`@Component` decorator function](../latest/api/core/index/Component-decorator
.html).
* **A @Component #{_decorator}** that associates *metadata* with the
`AppComponent` component class:
2016-08-09 12:38:25 -04:00
2016-07-27 03:24:54 -04:00
- a *selector* that specifies a simple CSS selector for an HTML element that represents
the component
- a *template* that tells Angular how to render the component's view.
* **A component class** that controls the appearance and behavior of a view
through its template. Here, you only have the root component, `AppComponent`. Since you don't
need any application logic in the simple QuickStart example, it's empty.
2016-08-09 12:38:25 -04:00
2016-07-27 03:24:54 -04:00
You *export* the `AppComponent` class so that you can *import* it into the application that you
just created.
2016-08-09 12:38:25 -04:00
2016-07-27 03:24:54 -04:00
Edit the file `app/app.module.ts` to import your new `AppComponent` and add it in the
declarations and bootstrap fields in the `NgModule` decorator:
2016-08-10 16:32:07 -04:00
2016-09-01 11:24:15 -04:00
+makeExample('app/app.module.ts', null, title='app/app.module.ts')
2016-08-09 12:38:25 -04:00
.l-main-section
2016-07-27 03:24:54 -04:00
h1#main Step !{step++}: Start up your application
2016-05-14 20:40:52 -04:00
block create-main
2016-08-10 16:32:07 -04:00
:marked
2016-07-27 03:24:54 -04:00
Now you need to tell Angular to start up your application.
2016-08-10 16:32:07 -04:00
Create the file `app/main.ts` with the following content:
2016-05-14 20:40:52 -04:00
2016-06-02 12:06:03 -04:00
+makeExample('app/main.ts')
2016-02-10 17:58:05 -05:00
2016-05-16 20:10:19 -04:00
2016-07-27 03:24:54 -04:00
- var _pBD_bootstrapModule = _docsFor == 'dart' ? _bootstrapModule : 'platformBrowserDynamic().bootstrapModule'
2016-05-16 20:10:19 -04:00
2016-07-27 03:24:54 -04:00
:marked
This code initializes the platform that your application runs in, then uses the platform to
bootstrap your `!{_AppModuleVsAppComp}`.
2016-08-10 16:32:07 -04:00
2016-07-27 03:24:54 -04:00
### Why create separate *<span ngio-ex>main.ts</span>*<span if-docs="ts">, app module</span> and app component files?
2016-05-16 20:10:19 -04:00
2016-07-27 03:24:54 -04:00
App bootstrapping is a separate concern from<span if-docs="ts"> creating a module or</span>
presenting a view. Testing the component is much easier if it doesn't also try to run the entire application.
2016-05-16 20:10:19 -04:00
2016-07-27 03:24:54 -04:00
.callout.is-helpful
header Bootstrapping is platform-specific
2016-05-16 20:10:19 -04:00
2016-07-27 03:24:54 -04:00
:marked
Because the QuickStart application runs directly in the browser, `main.ts` imports the
`!{_platformBrowserDynamicVsBootStrap}` function from `#{_angular_browser_uri}`, not
`#{_angular_core_uri}`. On a mobile device, you might load a !{_moduleVsComp} with
[Apache Cordova](https://cordova.apache.org/) or
[NativeScript](https://www.nativescript.org/), using a bootstrap function that's specific
to that platform.
2016-02-12 20:54:22 -05:00
2015-08-08 16:55:53 -04:00
.l-main-section
2016-07-27 03:24:54 -04:00
h1#index Step !{step++}: Define the web page that hosts the application
2015-12-10 21:29:14 -05:00
:marked
2016-07-27 03:24:54 -04:00
In the *#{_indexHtmlDir}* folder,
2016-05-16 20:10:19 -04:00
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
2015-08-08 16:55:53 -04:00
2016-07-27 03:24:54 -04:00
+ifDocsFor('ts')
2016-03-13 15:50:50 -04:00
:marked
2016-07-27 03:24:54 -04:00
The noteworthy sections here are:
* JavaScript libraries: `core-js` polyfills for older browsers, the `zone.js` and
`reflect-metadata` libraries needed by Angular, and the `SystemJS` library for module loading.
* Configuration file for `SystemJS`, and a script
where you import and run the `app` module which refers to the `main` file that you just
wrote.
* The [`<my-app>`](#my-app) tag in the `<body>` which is *where your app lives!*
:marked
## Add some style
Styles aren't essential, but they're nice, and `index.html` assumes that you have
2016-03-13 15:50:50 -04:00
a stylesheet called `styles.css`.
2016-05-16 20:10:19 -04:00
2016-07-27 03:24:54 -04:00
Create a `styles.css` file in the *#{_indexHtmlDir}* folder, and start styling,
2016-08-10 16:32:07 -04:00
perhaps with the minimal styles shown below.
2016-05-14 20:40:52 -04:00
2016-08-10 16:32:07 -04:00
+makeExcerpt('styles.css (excerpt)', 'quickstart')
.callout.is-helpful
:marked
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).
.l-main-section#build-and-run
2016-07-27 03:24:54 -04:00
h1 Step !{step++}: Build and run the application
2016-05-14 20:40:52 -04:00
block run-app
:marked
Open a terminal window and enter this command:
code-example.
npm start
2016-07-27 03:24:54 -04:00
aside.is-right
2016-05-14 20:40:52 -04:00
:marked
2016-07-27 03:24:54 -04:00
[Read more](https://github.com/angular/quickstart/blob/master/README.md#npm-scripts) about
other useful npm scripts included in this example's `package.json`.
:marked
That command runs the following two parallel node processes:
* The TypeScript compiler in watch mode
* 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-07-27 03:24:54 -04:00
In a few moments, a browser tab should open and display the following:
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-08-08 16:55:53 -04:00
2016-05-14 20:40:52 -04:00
block build-app
//- Nothing for ts.
2016-07-27 03:24:54 -04:00
.l-main-section#make-changes
h1 Step !{step++}: Make some live changes
2016-05-14 20:40:52 -04:00
:marked
2016-07-27 03:24:54 -04:00
Try changing the message in `app/app.component.ts` 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
2016-07-27 03:24:54 -04:00
The TypeScript compiler and `lite-server` will detect your change, recompile the TypeScript into JavaScript,
refresh the browser, and display your revised message.
2015-12-10 21:29:14 -05:00
2016-07-27 03:24:54 -04:00
Close the terminal window when you'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
2016-07-27 03:24:54 -04:00
h1#wrap-up Wrap up and next steps
2015-12-10 21:29:14 -05:00
:marked
2016-07-27 03:24:54 -04:00
The 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
2016-08-09 12:38:25 -04:00
.file app.module.ts
2016-05-14 20:40:52 -04:00
.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-07-27 03:24:54 -04:00
To see the file contents, open the <live-example></live-example>.
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?
2016-07-27 03:24:54 -04:00
This first application doesn't do much. It's basically "Hello, World" for Angular 2.
2015-10-14 23:25:19 -04:00
2016-07-27 03:24:54 -04:00
You wrote a little Angular component, created a simple `index.html`, and launched with a
static file server.
2015-08-08 16:55:53 -04:00
2016-08-10 16:32:07 -04:00
+ifDocsFor('ts')
2016-05-14 20:40:52 -04:00
:marked
2016-07-27 03:24:54 -04:00
You also created the basic application setup that you'll re-use for other
sections in this guide. From here, the changes you'll make in the
`package.json` or `index.html` files are only minor updates to add libraries or some css
stylesheets. You also won't need to revisit module loading again.
2016-05-14 20:40:52 -04:00
:marked
2016-07-27 03:24:54 -04:00
To take the next step and build a small application that demonstrates real features that you can
build with Angular, carry on to the [Tour of Heroes tutorial](./tutorial)!