Intro to Angular2
Full-time Firebase. Part-time Angular.
Angular2 is a work in progress.
Things might/will change.
Motivation
Concepts
Code
The code base for Angular dates back to 2009
Shadow DOM replaces Transclusion
ES6 Modules replaces Angular Modules
"Today, Angular2 is 5x faster than Angular 1"
- Misko, 2/10/15
Angular 1.x has many concepts
Concepts have a learning curve
Controller
Factory
Service
Provider
Directive
Transclusion
Module
Scope
Web Standards
Performance
Simple Cognitive Model
Your not-so first look...
Angular2
@Component({ selector: 'todo-app', template: new TemplateConfig({ url: '/todos.html' }) }) class TodoApp { todos; constructor() { this.todos = ['Item1', 'Item2']; } }
Provides syntax sugar for meta-data annotations
@Component({ selector: 'foo', template: new TemplateConfig({ url: '/foo.html' }) }) class Foo { }
AtScript is NOT a language to replace JavaScript
AtScript is merely syntactical sugar on top of ES6
Working with TypeScript, V8, and others on proposals for TC39 standards committe.
Compiles to ES5
Not required for Angular2
Types are optional
Components
@Component({ selector: 'todo-app', template: new TemplateConfig({ url: '/todos.html' }) }) class TodoApp { todos; constructor() { this.todos = ['Item1', 'Item2']; } }
Component Annotation
@Component({ selector: 'todo-app', template: new TemplateConfig({ url: '/todos.html' }) })
Component Controller
class TodoApp { todos; constructor() { this.todos = ['Item1', 'Item2']; } }
Components
@Component({ selector: 'todo-app', template: new TemplateConfig({ url: '/todos.html' }) }) class TodoApp { todos; constructor() { this.todos = ['Item1', 'Item2']; } }
Bootstrapping
Angular 1.x
var app = angular.module('app', []);
<body ng-app="app"> </body>
app.controller('MyCtrl', function($scope) { $scope.name = 'David'; });
<body ng-app="app"> <div ng-controller="MyCtrl"> My name is {{ name }} </div> </body>
Angular2 Bootstrapping
Create Component
@Component({ selector: 'todo-app', template: new TemplateConfig({ url: '/todos.html' }) }) export class TodoApp { todos; constructor() { this.todos = ['Item1', 'Item2']; } }
bootstrap
import {bootstrap} from 'angular'; import {TodoApp} from 'todoapp'; bootstrap(TodoApp);
ES6/AtScript compilation
Use Traceur (experimental options for AtScript)
In Browser or CLI
Working on key-turn tooling solution
Template Syntax
@Component({ selector: 'name-change', template: new TemplateConfig({ url: 'name-change.html' }) }) class NameChange { name; constructor() { this.name = ''; } changeName(newName) { this.name = newName; } }
<div> My name is {{ name }} </div> <div> <input #newname type="text"> <button (click)="changeName(newname.value)" [disabled]="newname.value == 'David'"> Change Name </button> </div>
Local Variables
<div> <input #newname type="text"> {{ newname.value }} </div>
Event Handlers
<div> <input #newname type="text"> <button (click)="changeName($event, newname.value)"> </div>
Property Bindings
<div> <input #newname type="text"> <span [textContent]="newname.value"></span> </div>
@Component({ selector: 'name-change', template: new TemplateConfig({ url: 'name-change.html' }) }) class NameChange { name; constructor() { this.name = ''; } changeName(newName) { this.name = newName; } }
<div> My name is {{ name }} </div> <div> <input #newname type="text"> <button (click)="changeName(newname.value)" [disabled]="newname.value == 'David'"> Change Name </button> </div>
(event) - for events
[property] - for properties
Angular 1.x
<div> My name is {{ name }} </div> <div> <input type="text" ng-model="name"> <button ng-name-change="changeName(name)">Change Name</button> </div>
Angular2
<div> My name is {{ name }} </div> <div> <input #newname type="text"> <button (namechange)="changeName(newname.value)">Change Name</button> </div>
Change Detection
An Angular2 application is a tree of components Victor Savkin's Blog Post
Zone.js
Informs Angular when to run change detection
No more $timeout!
app.controller('UserCtrl', function($scope, $timeout) { var ref = new Firebase('https://<my-firebase>.firebaseio.com/user/1'); ref.on('value', function(snap) { $timeout(function() { $scope.user = snap.val(); }); }); });
No Documentation, yet...
https://github.com/davideast/ng2do
Much more to be said at ng-conf