2015-04-19 21:12:19 -04:00
.l-main-section
2015-04-22 08:25:15 -04:00
2015-05-05 01:14:09 -04:00
h2#section-install-or-plunker Install Angular
2015-04-19 21:12:19 -04:00
p There are four steps to create any Angular app:
ol
li Create an entry point HTML file where users will start
li Load the Angular library at the top of the file
li Make a root component for your application
li Bootstrap Angular
p.
2015-05-05 01:14:09 -04:00
You can edit and test out your apps by serving local files with a web server. Follow the steps in the <a href="../quickstart.html">quickstart</a> to get Typescript setup.
2015-04-22 02:48:47 -04:00
2015-04-19 21:12:19 -04:00
p.
2015-08-27 19:03:15 -04:00
If you don't already have an HTTP server,
you can install one using <code>npm install -g http-server</code>.
(If that results in an access error, then you might need to use
<code><b>sudo</b> npm ...</code>.)
p For example:
code-example.
# From the directory that contains index.html:
npm install -g http-server # Or sudo npm install -g http-server
http-server # Creates a server at localhost:8080
# In a browser, visit localhost:8080/index.html
2015-04-22 02:48:47 -04:00
2015-05-05 01:14:09 -04:00
.callout.is-helpful
header Typescript vs ES5
p.
Although we work through the examples in TypeScript, you can also use
regular ES5. Click the ES5 link in any code box to see the ES5 JavaScript
version. Note that in ES5, you'd want to name your files <code>.js</code> rather than
<code>.ts</code>.
2015-04-18 12:12:05 -04:00
.l-main-section
2015-04-19 21:12:19 -04:00
h2#section-create-an-entry-point Create an entry point
p.
2015-05-05 01:14:09 -04:00
Create an <code>index.html</code> file and add the Angular library tags and a <code>main.ts</code> file where
2015-04-19 21:12:19 -04:00
you'll build your first component.
2015-04-18 12:12:05 -04:00
p.
2015-04-19 21:12:19 -04:00
In the <code><body></code>, add an element called <code><my-app></code> that will be the root of your
application.
2015-04-22 02:48:47 -04:00
2015-04-21 19:47:51 -04:00
p.
2015-05-05 01:14:09 -04:00
The TypeScript setup includes System.js, a third-party open-source library that adds ES6 module loading functionality to browsers. This step isn't needed for the ES5 version.
2015-04-19 21:12:19 -04:00
2015-05-19 09:33:09 -04:00
code-tabs
code-pane(language="html" name="TypeScript" format="linenums").
<!DOCTYPE html>
<html>
<head>
2015-06-04 07:02:10 -04:00
<script src="https://github.jspm.io/jmcriffey/bower-traceur-runtime@0.0.87/traceur-runtime.js"></script>
2015-05-19 09:33:09 -04:00
<script src="https://jspm.io/system@0.16.js"></script>
2015-06-04 06:51:01 -04:00
<script src="https://code.angularjs.org/2.0.0-alpha.26/angular2.dev.js"></script>
2015-05-19 09:33:09 -04:00
</head>
<body>
<my-app></my-app>
<script>
System.import('main');
</script>
</body>
</html>
code-pane(language="html" name="ES5" format="linenums").
<!DOCTYPE html>
<html>
<head>
2015-06-04 06:51:01 -04:00
<script src="https://code.angularjs.org/2.0.0-alpha.26/angular2.sfx.dev.js"></script>
2015-05-19 09:33:09 -04:00
<script src="main.js"></script>
</head>
<body>
<my-app></my-app>
</body>
</html>
2015-04-19 21:12:19 -04:00
.callout.is-helpful
header Don't use code.angularjs.org in a live app
p.
This example serves the Angular library from <a href="http://code.angularjs.org">code.angularjs.org</a>. This is
fine for examples, but you'd want to serve it yourself or use a CDN for real deployment.
2015-04-18 12:12:05 -04:00
.l-main-section
2015-04-19 21:12:19 -04:00
h2#section-set-up-the-starting-component Set up the starting component
p.
2015-05-05 01:14:09 -04:00
In <code>main.ts</code>, create a class called <code>AppComponent</code>, configure it to bind to the
2015-04-19 21:12:19 -04:00
<code><my-app></code> element in <code>index.html</code>, and call Angular's <code>bootstrap()</code> to kick
it all off like this:
2015-04-18 12:12:05 -04:00
2015-05-19 09:33:09 -04:00
code-tabs
code-pane(language="javascript" name="TypeScript" format="linenums").
import {Component, View, bootstrap} from 'angular2/angular2';
@Component({
selector: 'my-app'
})
@View({
template: '<h1>My first Angular 2 App</h1>'
})
class AppComponent {
}
bootstrap(AppComponent);
code-pane(language="javascript" name="ES5" format="linenums").
2015-04-22 09:59:02 -04:00
function AppComponent() {}
2015-04-22 08:33:17 -04:00
2015-04-22 09:59:02 -04:00
AppComponent.annotations = [
2015-05-05 01:14:09 -04:00
new angular.ComponentAnnotation({
2015-04-22 08:33:17 -04:00
selector: 'my-app'
2015-04-22 09:59:02 -04:00
}),
2015-05-05 01:14:09 -04:00
new angular.ViewAnnotation({
2015-04-22 08:33:17 -04:00
template: '<h1>My first Angular 2 App</h1>'
})
2015-04-22 09:59:02 -04:00
];
2015-04-22 08:33:17 -04:00
2015-04-22 09:59:02 -04:00
document.addEventListener('DOMContentLoaded', function() {
angular.bootstrap(AppComponent);
});
2015-04-22 08:33:17 -04:00
2015-05-05 01:14:09 -04:00
.callout.is-helpful
header Annotations vs Decorators
p.
If you are transpiling using a tool that translates the <code>@</code> symbols to
annotations (for example Traceur), you will need to import the annotation versions of
Component and View. That can be easily achieved using
<code>import {ComponentAnnotation as Component, ViewAnnotation as View}</code>.
2015-04-22 02:48:47 -04:00
2015-04-19 21:12:19 -04:00
.l-main-section
h2#section-run-it Run it!
p.
2015-05-05 01:14:09 -04:00
Open <code>index.html</code> through your web server and you should see:
2015-05-26 09:29:27 -04:00
figure.image-display
img(src='/resources/images/examples/setup-example1.png' alt="Example of Todo App")
2015-04-18 12:12:05 -04:00
.l-main-section
2015-04-19 21:12:19 -04:00
h2#section-explanations Explanations
p This basic Angular app contains the structure for any app you'll build.
2015-04-21 19:47:51 -04:00
.l-sub-section
h3 It's all a tree
p.
You can think of Angular apps as a tree of components. This root component we've been talking about acts as the top
level container for the rest of your application. You've named this one <code>AppComponent</code>, but there's
nothing special about the name and you can use whatever makes sense to you.
p.
The root component's job is to give a location in the <code>index.html</code> file where your application will
2015-05-19 15:06:40 -04:00
render through its element, in this case <code><my-app></code>. There is also nothing special about this
2015-05-19 15:07:46 -04:00
element name; you can pick it as you like.
2015-04-21 19:47:51 -04:00
p.
The root component loads the initial template for the application that will load other components to perform
whatever functions your application needs - menu bars, views, forms, etc. We'll walk through examples of all of
these in the following pages.
2015-04-19 21:12:19 -04:00
2015-04-21 19:47:51 -04:00
.l-sub-section
h3 @Component and @View annotations
p.
A component annotation describes details about the component. An annotation can be identified by its at-sign (<code>@</code>).
p.
The <code>@Component</code> annotation defines the HTML tag for the component by specifying the component's CSS selector.
p.
The <code>@View</code> annotation defines the HTML that represents the component. The component you wrote uses an inline template, but you can also have an external template. To use an external template, specify a <code>templateUrl</code> property and give it the path to the HTML file.
2015-04-22 02:48:47 -04:00
2015-04-21 19:47:51 -04:00
.l-sub-section
h3 import vs. window.angular
p.
The main difference between the ES5 and TypeScript versions is the loading of modules.
2015-04-22 02:48:47 -04:00
strong TypeScript
2015-04-21 19:47:51 -04:00
p.
TypeScript supports ES6 module loading syntax. ES6 modules allow for modular loading of JavaScript code. Using ES6 modules you can cherry-pick only what you need for your app.
2015-04-22 02:48:47 -04:00
2015-04-21 19:47:51 -04:00
strong ES5
p.
2015-04-22 02:48:47 -04:00
In ES5 the script file creates an angular property on the window of the browser. This property contains every piece of Angular core, whether you need it or not.
2015-05-19 09:33:09 -04:00
code-tabs
code-pane(language="javascript" name="TypeScript" format="linenums" ).
import {Component, View, bootstrap} from 'angular2/angular2';
...
// bootstrap is available for use because it was imported from angular core
bootstrap(AppComponent);
code-pane(language="javascript" name="ES5" format="linenums").
// window.angular is available because the script file attaches the angular property to the window
document.addEventListener('DOMContentLoaded', function() {
angular.bootstrap(AppComponent);
});