Created by Victor Mejia
Software Engineer @ CoStar Group
Tweeting @_victormejia
Coding @victormejia
Angular2 is a work in progress.
Things might/will change.
I'm not an Angular 2 expert...yet :)
The code base for Angular dates back to 2009
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
Object.observe()
ES6
Web Components
Classes
Modules
Arrow Functions
class User { constructor(email) { this.email = email; } savePassword(pwd) { this.password = pwd; } } var User = new User('hithere@email.com'); user.savePassword('easyPassword');
function User(email) { this.email = email; } User.prototype.savePassword = function (pwd) { this.password = pwd; } var User = new User('hithere@email.com'); user.savePassword('easyPassword');
// 'services/TodoService.js' export class TodoService { constructor() { this.todos = []; } add(todo) { this.todos.push(todo); } }
import {TodoService} from 'services/TodoService';
Your not-so first look...
Angular2
@Component({ selector: 'todo-app', }) @View({ templateUrl: 'todo.html', }) class TodoApp { todos; constructor() { this.todos = ['Item1', 'Item2']; } }
Provides syntax sugar for meta-data annotations
@Component({ selector: 'foo', }) @View({ templateUrl: 'foo.html' }) class Foo { }
Superset of JavaScript
It's a transpiler + develoment experience
Providing annotation support in the latest alpha
Angular team working with TypeScript team on proposals for TC39 standards committe.
Compiles to ES5
Not required for Angular2
Types are optional
Components
@Component({ selector: 'todo-app', }) @View({ templateUrl: 'todos.html' }) class TodoApp { todos; constructor() { this.todos = ['Item1', 'Item2']; } }
Component Annotation
@Component({ selector: 'todo-app', }) @View({ templateUrl: 'todos.html' })
Component Controller
class TodoApp { todos; constructor() { this.todos = ['Item1', 'Item2']; } }
Components
@Component({ selector: 'todo-app', }) @View({ templateUrl: '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 = 'Victor'; });
<body ng-app="app"> <div ng-controller="MyCtrl"> My name is {{ name }} </div> </body>
Angular2 Bootstrapping
Create Component
@Component({ selector: 'todo-app' }) @View({ templateUrl: 'todos.html' }) class TodoApp { todos; constructor() { this.todos = ['Item1', 'Item2']; } }
bootstrap
import {bootstrap} from 'angular'; import {TodoApp} from 'todoapp'; bootstrap(TodoApp);
Use TypeScript Transpiler
In Browser or CLI
Working on key-turn tooling solution
Template Syntax
@Component({ selector: 'name-change' }) @View({ templateUrl: '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 - DOM Reference
<div> <input #newname type="text" (keyup)="updateName($event, newname)"> {{ name }} </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' }) @View({ templateUrl: '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
#view-reference
Angular 1.x
<div> My name is {{ name }} </div> <div> <input type="text" ng-model="name"> <button ng-click="changeName(name)">Change Name</button> </div>
Angular2
<div> My name is {{ name }} </div> <div> <input #newname type="text"> <button (click)="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(); }); }); });
Still very experimental, things changing a lot
Router not fully ported/documented yet
Forms module has not matured