On Github thoughtram / angular-upgrade-slides
We will focus on upgrading an existing (legacy) app by example.
There some things we can do today to make upgrading our application easier:
There are two different ways of how to upgrade an existing app to Angular 2:
For this to work, four things need to interoperate between Angular 1 and Angular 2:
import {UpgradeAdapter} from '@angular/upgrade'; export const upgradeAdapter = new UpgradeAdapter();
import {UpgradeAdapter} from '@angular/upgrade'; export const upgradeAdapter = new UpgradeAdapter();
import {upgradeAdapter} from 'upgrade-adapter'; angular.module('contacts-app', [...]); upgradeAdapter .bootstrap(document.body, ['contacts-app']);
import {upgradeAdapter} from 'upgrade-adapter'; angular.module('contacts-app', [...]); upgradeAdapter .bootstrap(document.body, ['contacts-app']);
@Component({ selector: 'contacts-list-item-component', template: ` <a href="#/contact/{{contact.id}}"> <img [src]="contact.image"> <span>{{contact.name}}</span> </a> ` }) export class ContactsListItemComponent { @Input() contact: Contact; }
import {upgradeAdapter} from 'upgrade-adapter'; import {ContactsListItemComponent} from '...'; angular.module('contacts-list-item-component', []) .directive('contactsListItemComponent', upgradeAdapter .downgradeNg2Component(ContactsListItemComponent) );
import {upgradeAdapter} from 'upgrade-adapter'; import {ContactsListItemComponent} from '...'; angular.module('contacts-list-item-component', []) .directive('contactsListItemComponent', upgradeAdapter .downgradeNg2Component(ContactsListItemComponent) );
import {upgradeAdapter} from 'upgrade-adapter'; import {ContactsListItemComponent} from '...'; angular.module('contacts-list-item-component', []) .directive('contactsListItemComponent', upgradeAdapter .downgradeNg2Component(ContactsListItemComponent) );
angular.module('contacts-app', [ ... 'contacts-list-item-component' ]);
<ul> <li ng-repeat="contact in contacts"> <contact-list-item-component [contact]="contact"> </contact-list-item-component> </li> </ul>
<ul> <li ng-repeat="contact in contacts"> <contact-list-item-component [contact]="contact"> </contact-list-item-component> </li> </ul>
angular.module('zippy-component', []) .component('zippyComponent', { templateUrl: '...', transclude: true, bindings: { closed: '<', title: '@', toggle: '&' }, controller: function () { ... } })
@Component({ selector: 'contact-detail-component', templateUrl: '...', }) export class ContactDetailComponent { ... }
import {upgradeAdapter} from 'upgrade-adapter'; const ZippyComponent = upgradeAdapter.upgradeNg1Component('zippyComponent'); @Component({ selector: 'contact-detail-component', templateUrl: '...', directives: [ZippyComponent] }) export class ContactDetailComponent { ... }
import {upgradeAdapter} from 'upgrade-adapter'; const ZippyComponent = upgradeAdapter.upgradeNg1Component('zippyComponent'); @Component({ selector: 'contact-detail-component', templateUrl: '...', directives: [ZippyComponent] }) export class ContactDetailComponent { ... }
<zippy-component [title]="zippyCaption" (toggle)="toggleCaption($event.closed)"> </zippy-component>
<zippy-component [title]="zippyCaption" (toggle)="toggleCaption($event.closed)"> </zippy-component>
@Component() export class ContactDetailComponent { contact: Contact; constructor() { this.contact = contactsService.getContact($routeParams.id); } }
@Component() export class ContactDetailComponent { contact: Contact; constructor() { this.contact = contactsService.getContact($routeParams.id); } }
upgradeAdapter.upgradeNg1Provider('contactsService'); upgradeAdapter.upgradeNg1Provider('$routeParams'); @Component() export class ContactDetailComponent { contact: Contact; constructor( @Inject('contactsService') contactsService, @Inject('$routeParams') $routeParams) { this.contact = contactsService.getContact($routeParams.id); } }
upgradeAdapter.upgradeNg1Provider('contactsService'); upgradeAdapter.upgradeNg1Provider('$routeParams'); @Component() export class ContactDetailComponent { contact: Contact; constructor( @Inject('contactsService') contactsService, @Inject('$routeParams') $routeParams) { this.contact = contactsService.getContact($routeParams.id); } }
import {Injectable} from '@angular/core'; @Injectable() class ContactsService { private CONTACT_DATA = [...]; getContacts() { return this.CONTACT_DATA; } getContact(id: string) { return this.CONTACT_DATA .find(contact => contact.id === id); } }
import {upgradeAdapter} from 'upgrade-adapter'; angular.module('contacts-service', []) .service('contactsService', upgradeAdapter.downgradeNg2Provider(ContactsService) );
import {upgradeAdapter} from 'upgrade-adapter'; angular.module('contacts-service', []) .service('contactsService', upgradeAdapter.downgradeNg2Provider(ContactsService) );
@Component() export class ContactDetailComponent { contact: Contact; constructor( @Inject('contactsService') contactsService, @Inject('$routeParams') $routeParams) { this.contact = contactsService.getContact($routeParams.id); } }
@Component() export class ContactDetailComponent { contact: Contact; constructor( @Inject('contactsService') contactsService @Inject('$routeParams') $routeParams) { this.contact = contactsService.getContact($routeParams.id); } }
upgradeAdapter.addProvider(ContactsService); @Component() export class ContactDetailComponent { contact: Contact; constructor( contactsService: ContactsService, @Inject('$routeParams') $routeParams) { this.contact = contactsService.getContact($routeParams.id); } }
upgradeAdapter.addProvider(ContactsService); @Component() export class ContactDetailComponent { contact: Contact; constructor( contactsService: ContactsService, @Inject('$routeParams') $routeParams) { this.contact = contactsService.getContact($routeParams.id); } }