29
Ross Dederer, May 2016 Migrate from Angular 1 to Angular 2

Migrating an application from Angular 1 to Angular 2

Embed Size (px)

Citation preview

Page 1: Migrating an application from Angular 1 to Angular 2

Ross Dederer, May 2016

Migrate from Angular 1 to Angular 2

Page 2: Migrating an application from Angular 1 to Angular 2

about: me Ross Dederer, Developer Relations Wijmo specializes in JavaScript UI controls

o Grids, FlexSheet o Charts, Financial Charts, Gauges o Inputs, etco OLAP

Supported AngularJS since the early days and now offer full Angular 2 support.

Wijmo Team always researching new frameworks o React and Ember coming soon

Page 3: Migrating an application from Angular 1 to Angular 2

about: discussion What is Angular 2? TypeScript

o Introduction o IntelliSense

Angular 2: 8 Building Blocks Data Services Quick Start – Migration Story: Silverlight to HTML5 Migrating View to Angular 2 template Migrating ViewModel to Angular 2

Page 4: Migrating an application from Angular 1 to Angular 2

about: Silverlight App

Page 5: Migrating an application from Angular 1 to Angular 2

about: HTML5 AngularJS App

Page 6: Migrating an application from Angular 1 to Angular 2

Angular 2: introduction Modular

o Smaller Core

Moderno Evergreen browsers using ECMA 6 o Legacy Browsers supported

Component-Based Developed using TypeScript Documentation has two tracks Google will continue developing Angular 1.x

o Wijmo will continue to support it

Page 7: Migrating an application from Angular 1 to Angular 2

TypeScript: introduction TypeScript was created by Microsoft ( FOSS ) Google is fully adopting TypeScript for Angular 2 development Superset of JavaScript that compiles to plain JS

o Any browser, any host, any OS

Offers classes, modules, and interfaces Object-Oriented with Classes, etc. Type Safety Find errors at design time

Page 8: Migrating an application from Angular 1 to Angular 2

IntelliSense Feature of TypeScript

o Not exclusive to Angular 2

Familiar experience for experienced C# developers Self-documentation

Page 9: Migrating an application from Angular 1 to Angular 2

Angular 1 vs. Angular 2: Visual Studio

Angular 1

JavaScript

Angular 2

TypeScript

Page 10: Migrating an application from Angular 1 to Angular 2

Angular 2: first impressions from Wijmo devs Better Encapsulation – Modular Design

o Components

Angular 2 seems more natural than Angular 1o Delegates tasks to JS interpreter. o Leverages HTML5 more efficiently

Typescript Integration o IntelliSense

No $scope or $watch

Page 11: Migrating an application from Angular 1 to Angular 2

Diagram from angular.io

Page 12: Migrating an application from Angular 1 to Angular 2

Module Application is a collection of Modules Angular provides us built-in modules Dedicated to single purpose Expose “things” – classes, functions, values

o Other modules import these “things”

import {PortingFromSL} from './app';

import {Component} from 'angular2/core';

export class PortingFromSL{ }

Page 13: Migrating an application from Angular 1 to Angular 2

Component Define a module as component by attaching Meta Data to plain old TS

class Controls patch of screen – View Contains our class that interacts with the view

o Via API of properties and methods.

Each Component has a companion Template “Code-behind” for components View Angular creates updates and destroys components

Page 14: Migrating an application from Angular 1 to Angular 2

Templating Components templateUrl contains component Template A form of HTML (not an HTML Form) Introduces new syntax

o *ngIf , *ngFor

Angular manages the component and its view (template) All HTML is valid Template Synatx

Page 15: Migrating an application from Angular 1 to Angular 2

Metadata and Decorators Decorators

o @component

Metadata o Selector o TemplateUrl o Directives o Providers

The template, metadata, and component together describe the view.

Page 16: Migrating an application from Angular 1 to Angular 2

Data Binding: overview Interpolation {{ value }}

Template Expressions [property]=“expression”o Produces a value

Template Statements (event)=“statement”o Responds to event

Two-way Data Binding [(target)] = “expression”

Page 17: Migrating an application from Angular 1 to Angular 2

Data Services Application

Page 18: Migrating an application from Angular 1 to Angular 2

Data Services Quick Start Application Loading data into Wijmo CollectionView

o Sort, filter, group

Managing hierarchical data to reflect current state o Master Detail Relationships

Binding data to Wijmo controls o Line of business application template – Grid’s inputs

Live Demo: http://demos.wijmo.com/5/Angular/PortingFromSL/PortingFromSL/

Page 19: Migrating an application from Angular 1 to Angular 2

Application Structure: new things In Ng 1, we needed to create a new app and give it a

controller to handle all of the business logic. o No more controllers in Ng 2

In Ng 2, we will organize our app inside a module component that we will call PortingFromSL o We will leverage the benefits of TypeScript here

In Ng 2, we will move all of our markup and templating sits in app.html

In Ng 2, we need to introduce a root component loader

Page 20: Migrating an application from Angular 1 to Angular 2

Migration from Angular 1 to Angular 2 Convert JavaScript to TypeScript

o Not required but I recommend it

Keep familiar MVVM development pattern Porting the ViewModel — Component

o No controller?o No $scope?

Porting the View o New attribute syntax

Page 21: Migrating an application from Angular 1 to Angular 2

Bootstrapping - Launching ApplicationFrom bootstrap.ts :

import {class} from './app';

bootstrap(class.component);

Inside default.htm:

System.import(“/src/class.component”);

Page 22: Migrating an application from Angular 1 to Angular 2

Porting the Model We luckily will use the same model/Datasource

o JSON coming from service can serve as model.

private _svcUrl: string = 'http://services.odata.org/Northwind/Northwind.svc';

Page 23: Migrating an application from Angular 1 to Angular 2

Porting the ViewModel CollectionView objects for Customers, Orders, Details Load some data using AJAX Ng 2 More closely related to SL than NG1

Class App

customers: wijmo.collections.CollectionView; // or any

Constructorthis.customers = new wijmo.collections.CollectionView();

Page 24: Migrating an application from Angular 1 to Angular 2

Porting the view: html New Template Syntax Updated Bindings Need to add placeholder in default.html

In Angular 1: In Angular 2 :

Page 25: Migrating an application from Angular 1 to Angular 2

Porting the view: wijmo controlsAngular 2 code:

New Binding syntax New Template syntax Local Variables

Page 26: Migrating an application from Angular 1 to Angular 2

Migrating the ‘Controller’

export module portingFromSL { @Component({ selector: 'app', templateUrl: 'src/app.html', })export class App {

// business logic }

Page 27: Migrating an application from Angular 1 to Angular 2

Default.htm Selector

Everything is a component!

Think of a component as a single unit which is a model, a controller, and a view.

Page 28: Migrating an application from Angular 1 to Angular 2

Live Demo

Page 29: Migrating an application from Angular 1 to Angular 2

Conclusion Read more at:

o www.wijmo.com/angular2o Angular.io

Contact me at [email protected]