Angular 2
Chuck Dascalos
@dascalos
Why Migrate 1 to 2?
- Significantly faster
- Simpler - fewer types of objects involved
- Forward looking - supports newer front-end tools and techniques, built on web standards
- Supports additional features - lazy loading, multiple rendering targets
Why not Angular 2?
- Not complete
- Much more community support of Angular 1
Third-Party Support
- Angular 1 - Lots of third-party modules (ngmodules.org)
- Angular 2 - Not as extensive as Angular 1, no central repository, GitHub projects exists for Infinite Scroll, File Upload. Couldn't find an ngTable for Angular 2.
Upgrade Path
- Controllers, Services, and Directives can be upgraded to Angular 2. Controllers and directives become Components. A downgrade process allows them to be used in Angular 1
- Directives and Filters are not upgradeable. Must be rewritten as Angular 2 Directives and Pipes
- You can run Angular 1 and Angular 2 on the same page. (Must be on Angular 1.3 to do hybrid, preferably 1.5)
Detailed Migration Steps
- Follow the Style Guide
- Upgrade to at least Angular 1.3, preferably 1.5
- All new development with Components (1.5)
- Switch Controllers to Components
- Remove Incompatible Features from Directives
- Switch Component Directives to Components
- Implement Manual Bootstrapping
- Add TypeScript & a Build
- Start Using ES6
- Switch Controllers to ES6 Classes
- Switch Services to ES6 Classes
What's New in Angular 2
Angular 1
<body ng-controller="StoryController as vm">
<h3>{{vm.story.name}}</h3>
<h3 ng-bind="vm.story.name"></h3>
</body>
(function() {
angular
.module('app')
.controller('StoryController', StoryController);
function StoryController() {
var vm = this;
vm.story = { id: 100, name: 'The Force Awakens' }
}
})();
Angular 2
import { Component } from 'angular2/core';
@Component({
selector: 'my-story',
template: '<h3>{{story.name}}</h3>
})
export class StoryComponent {
story = { id: 100, name: 'The Force Awakens' }
}
Controllers to Components
Angular 1
<html ng-app="app">
Angular 2
import { Bootstrap } from 'angular2/platform/browser';
import { AppComponent } from './app.component';
bootstrap(AppComponent);
Bootstrapping Angular
Angular 1
<ul>
<li ng-repeat="vehicle in vm.vehicles">
{{ vehicle.name }}
</li>
</ul>
<div ng-if="vm.vehicles.length">
<h3>You have {{vm.vehicles.length}} vehicles</h3>
</div>
Angular 2
<ul>
<li *ngFor="#vehicle of vehicles">
{{ vehicle.name }}
</li>
</ul>
<div *ngIf="vehicles.length">
<h3>You have {{ vehicles.length }}</h3>
</div>
Structural Directives
Angular 1
<h3 ng-bind="vm.story.name"></h3>
Angular 2
<h3>{{story.name}}</h3>
<div [style.color]="color">{{story.name}}</div>
One-Way Data Binding
Angular 1
<button ng-click="vm.log('click')"
ng-blur="vm.log('blur')">OK</button>
Angular 2
<button (click)="log('click')" (blur)="log('blur')">OK</button>
Event Binding
Angular 1
<input ng-model="vm.story.name">
Angular 2
<input [(ngModel)]="story.name">
Two-Way Data Binding
Angular 1
<div ng-style="vm.story ? {visibility: 'visible'}
: { visibility: 'hidden'}">
<img ng-src="{{vm.imagePath}}">
<br/>
<a ng-href="{{vm.link}}">{{vm.story}}</a>
</div>
Angular 2
<div [style.visibility]="story ? 'visible' : 'hidden'">
<img [src]="imagePath">
<br/>
<a [href]="link">{{story}}</a>
</div>
Less Directives
Angular 1
angular
.module('app')
.service('VehicleService', VehicleService);
function VehicleService() {
this.getVehicles = function() {
return [
{ id: 1, name: 'X-Wing Fighter' },
{ id: 2, name: 'Tie Fighter' },
{ id: 3, name: 'Y-Wing Fighter' }
]
}
}
Angular 2
import {Injectable} from 'angular2/core'
@Injectable()
export class VehicleService {
getVehicles = () => [
{ id: 1, name: 'X-Wing Fighter' },
{ id: 2, name: 'Tie Fighter' },
{ id: 3, name: 'Y-Wing Fighter' }
];
}
Service
Angular 2
By Chuck Dascalos
Angular 2
- 497