Angular 2 (beta)
FED Brownbag

Toolbox
Like all frameworks today, setup requires additional modules.
- Node / npm
- Webpack
- (ES6)
- Typescript
- Angular 2 (beta v.12)



What is Webpack?
Webpack is a module bundler based on your app's dependencies into a single or multiple files.
It is also a task runner through the use of loaders.


What is ES6 (ES2015)?
It’s the next version of JavaScript, and it has some great new features (sugar).
let a = 0; // Block-level scoped only!
const b = { foo: 'bar' } // read-only REFERENCE to a value (not immutable)
(c) => c * 2
// equivalent
function(c) {
return c * 2;
}
/* String */
'foobar'.startsWith('foo'); //true
'foobar'.endsWith('foo'); // false
'foobar'.includes('bar'); // true
// String interpolation
let name = 'JavaScript';
console.log(`Hello world, love ${name}!`); // Prints "Hello world, love JavaScript!"
// equivalent
"Hello world, love " + name + "!"
What is TypeScript?
- Typed superset of JavaScript, transpiles down to ES5 JavaScript
- Developed by Microsoft, endorsed by Angular 2 devs
- Also includes some ES6 features
- Sometimes hard to distinguish
function greeter(person: string) {
return "Hello, " + person;
}
let name = "TypeScript";
console.log(greeter(name));
// Prints "Hello, TypeScript"
let numbers = [0, 1, 2];
console.log(greeter(numbers));
// Error: Supplied parameters do not
// match any signature of call target
Set up dev environment
git clone https://github.com/filoxo/ng2-webpack-starter.git
If cloning:
- Open package.json
- Change rxjs version to '5.0.0-beta.4'
- v2 is broken right now
https://github.com/ReactiveX/rxjs/issues/1595
- v2 is broken right now
- Then `npm install`
- mkdir ng2-profile
- cd ng2-profile
- npm init -y
- Install and save dependencies
- Create tsconfig.json
- Edit package.json, add scripts
- Create webpack.config.js
- npm start
npm i angular2@2.0.0-beta.12 es6-shim reflect-metadata rxjs@5.0.0-beta.2 zone.js -S
npm i webpack-dev-server ts-loader typings -D
npm i webpack typescript -g
- mkdir ng2-profile
- cd ng2-profile
- npm init -y
- Install and save dependencies
- Create tsconfig.json
- Edit package.json, add scripts
- Create webpack.config.js
- npm start
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
- mkdir ng2-profile
- cd ng2-profile
- npm init -y
- Install and save dependencies
- Create tsconfig.json
- Edit package.json, add scripts
- Create webpack.config.js
- npm start
"build": "webpack",
"start": "webpack-dev-server",
"typings": "typings",
"postinstall": "typings install"
- mkdir ng2-profile
- cd ng2-profile
- npm init -y
- Install and save dependencies
- Create tsconfig.json
- Edit package.json, add scripts
- Create webpack.config.js
- npm start
var webpack = require("webpack");
module.exports = {
entry: {
"vendor": "./src/vendor/vendor",
"app": "./src/main"
},
output: {
path: __dirname + "/dist",
filename: "[name].bundle.js"
},
resolve: {
extensions: ['',
'.webpack.js',
'.web.js',
'.ts',
'.js']
},
devtool: 'source-map',
module: {
loaders: [
{
test: /\.ts?$/,
loader: 'ts-loader'
}
]
},
plugins: [
new webpack.optimize.CommonsChunkPlugin(
/* chunkName= */"vendor",
/* filename= */"./vendor.bundle.js"
)
]
};
First component
- Component is imported as a class. AppComponent is exported as a class too.
- Exporting is what makes file a "module". Each file should correspond to only one module.
- @Component is a decorator. It will apply metadata to our class using the configuration object we are passing into it.
-
The configuration object has 2 properties: selector and template.
- The selector specifies a simple CSS selector for an HTML element named my-app.
- The template property defines the component's template using ES6 template strings. This template is a single H1 element containing the text"My First Angular App".
import { Component } from 'angular2/core';
@Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }
Bootstrapping
-
We are importing `bootstrap`, Angular2's method to starting your application.
- Bootstrapping is platform-specific (browser, webview, server)
- You can learn more about Angular's bootstrap function from their docs.
- Nothing to do Twitter's Bootstrap!
- We are also importing the AppComponent we just exported in our app.component file.
import { bootstrap } from 'angular2/platform/browser';
import { AppComponent } from './app.component';
bootstrap(AppComponent);
Add more info
- Add class members with type
- Bind those to template using {{ }}
- Use ES6 template string instead
import { Component } from 'angular2/core';
@Component({
selector: 'my-app',
template: `
<h1>{{ name }}</h1>
<h2>{{ email }}</h2>
`
})
export class AppComponent {
name: String = "";
email: String = "";
// test: number = 'asdf';
}
template string to templateUrl
- Files are relative to index.js, in this case /
- ES6 multiline string not necessary
import { Component } from 'angular2/core';
@Component({
selector: 'my-app',
templateUrl: 'src/components/app/app.tpl.html'
})
export class AppComponent {
name: String = "";
email: String = "";
}
JobList Component
- New directory in /src/components
- Create new file called joblist.component.ts
Job Component
- Create new file called job.component.ts
More
- Style Guide (WIP):
https://github.com/mgechev/angular2-style-guide - Angular Quickstart Project:
https://angular.io/docs/ts/latest/quickstart.html - Egghead.io video training
https://egghead.io/technologies/angular2 -
48 hour online Angular hackathon
https://www.angularattack.com/
FED Brownbag: Angular 2
By Carlos Filoteo
FED Brownbag: Angular 2
- 526