code migration from angular 1.x to angular 2.0

18
From 1.x to 2.0 Ran Wahle

Upload: ran-wahle

Post on 20-Jan-2017

153 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Code migration from Angular 1.x to Angular 2.0

From 1.x to 2.0 Ran Wahle

Page 2: Code migration from Angular 1.x to Angular 2.0

Agenda

A little about Angular 2.0 A little chat application with Angular 1.x Reorganizing modules Using Module loader Switching to typescript Using angular 1.5 components Using webpack as our module loader Make hybrid application with ng-2 Gradually swithing to ng-2

Page 3: Code migration from Angular 1.x to Angular 2.0

A little about myself

Full-stack developer Enthusiastic front-end developer Poor UI designer Participated in writing Developing an Angular 2 Edge

Page 4: Code migration from Angular 1.x to Angular 2.0

A little about Angular 2.0

A whole new rewrite No $scope (and no $scope.$apply/$applyAsync) A whole new change detection system No modules (not in the Angular 1.x way) Supports new standards

Page 5: Code migration from Angular 1.x to Angular 2.0

A little about rc-5

NgModule decorator AoT Compilation

Page 6: Code migration from Angular 1.x to Angular 2.0

What is our goal?

Gradually migrate Angular 1.x application During the process, enjoy Angular 2.0 capabilities

in our Angular 1.x application

Page 7: Code migration from Angular 1.x to Angular 2.0

What do we need?

Prepare our application Blend Angular 2.0 in Gradually upgrade

Page 8: Code migration from Angular 1.x to Angular 2.0

A little chat application from 2012

One module Every service, controller, directive, component and filter will be

registered to it. This one module is bootstraped to the document Uses socket.io for chating Needs to be migrated gradually (well, it did if it was big)

Page 9: Code migration from Angular 1.x to Angular 2.0

What have we done already

Used controllerAs syntax No function or field is declared directly on the scope

Page 10: Code migration from Angular 1.x to Angular 2.0

Restructuring modules

Every controller / directive / service will now have its own module Every module will have its own dependencies The main module depend on other modules

Page 11: Code migration from Angular 1.x to Angular 2.0

Load modules with AMD

Remove all script tags from the application’s HTML page Declare JS dependencies directly in JS files

Page 12: Code migration from Angular 1.x to Angular 2.0

Switch to TypeScript

Why? Actually we can rename our ‘js’ files to ‘ts’

and correct compiler errors in case there are All our controllers / components/ services can be switched to classes We may reduce the prototype

Page 13: Code migration from Angular 1.x to Angular 2.0

Using Angular 1.5 component

Why? This is the closest we can get to Angular 2.0 component Controller that is bound to an HTML page can be easily replaced with a

component and template Template directive, can be replaced even easier

Page 14: Code migration from Angular 1.x to Angular 2.0

Using WebPack

It is not necessary for the migration, but usefull nevertheless Our TS code can use “import“ statements On our tsConfig.json file we’ll u se “commonJs” as our module loader. Webpack will pack all files by the “require” statement transpiled from

the “import” statement.

Page 15: Code migration from Angular 1.x to Angular 2.0

Adding Angular 2.0 into the mix

Replace our bootstrap with UpgradeAdapeter Upgrade ng-1 service to an injectable ng-2 service Making the upgraded service available on ng-1

Page 16: Code migration from Angular 1.x to Angular 2.0

Upgrading components

Add the @Component decorator. Change the template to match ng2 data binding Export the component class. On the bootstraping TS file, downgrade it to ng-1

Page 17: Code migration from Angular 1.x to Angular 2.0

Summary

We have started with Angular 1.x code We have changed it structure Angular 1.x code can look very similar to ng-2 Migrating the code is not so difficult task Also, we can work with both ng-1 and ng-2 modules

side by side at the same application

Page 18: Code migration from Angular 1.x to Angular 2.0

Thank you

Ran [email protected]@ranwahleBlog: https://blogs.microsoft.co.il/ranw