Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Angular2 + TypeScript, or where is front-end development going to?

Przemek
October 19, 2016

Angular2 + TypeScript, or where is front-end development going to?

You can find docs about ng2 / ts literally everywhere. So let's forget about docs and let's just focus on broader view.

Przemek

October 19, 2016
Tweet

More Decks by Przemek

Other Decks in Programming

Transcript

  1. Components Isomorphic Apps Banana in a box Single-Page Apps JIT

    Webpack Two-way binding Angular CLI Mobile Routing Angular2 vs React Transpiling AOT DI TypeScript One-way binding Migration paths Redux Immutable structures Observables
  2. Strongly typed Enums Inheritance Typings Classes TypeScript vs Flow Used

    in Angular2 Tooling .d.ts Superset Interfaces Transpiling Generics Modules
  3. Stage I – DOM-based apps • 'We have the DOM.

    We should definitely use it.’ • Presentation mixed with business logic • JS criticized for propagation of bad practices
  4. bindEvents: function () { $('#new-todo').on('keyup', this.create.bind(this)); $('#toggle-all').on('change', this.toggleAll.bind(this)); $('#footer').on('click', '#clear-completed',

    this.destroyCompleted.bind(this)); $('#todo-list') .on('change', '.toggle', this.toggle.bind(this)) .on('dblclick', 'label', this.edit.bind(this)) .on('keyup', '.edit', this.editKeyup.bind(this)) .on('focusout', '.edit', this.update.bind(this)) .on('click', '.destroy', this.destroy.bind(this)); }
  5. Stage II – Model-View-Whatever • Let’s separate concerns! • Let’s

    take a look at the… back-end guys! • MV*-all-the-things
  6. <section class="todoContainer" ng-controller="TodoController as ctrl"> <input type="text" ng-model="ctrl.todoText"> <ul> <li

    ng-repeat="todoItem in ctrl.todoItems" ng-click="ctrl.toggleTodo(todoItem)"> </li> </ul> </section>
  7. Stage III – World of the Components • Front-end needs

    more granular approach to build apps • Data flow should be predictable • How to make our code more reusable?
  8. angular.directive('userProfile', function() { return { ... link: function(scope, element, attributes,

    controller) { var title = angular.element(element.children()[0]); ... } }; });
  9. • DOM manipulations and custom elements created by directives •

    Strong coupling with MV* approach (ng-controller) • Components introduced in Angular 1.5 Evolution of Front-End Architecture vs Angular 1.x
  10. • Single Page Applications • Mobile • Performance • Server-side

    rendering • And more… New ideas of front-end world
  11. Angular 1.x is good in most of these areas …but

    it wasn’t designed for them.
  12. • Maintenance costs are too high • Refactoring of projects

    greater than 10 LOC is 'a pain in the…' • Tools / IDEs have problems with dynamically typed languages What about JavaScript?
  13. • TypeScript 2.0 • Types, Interfaces, Classes, Generics, Enums, Modules,

    Decorators • More than just a compiler • Open sourced, community driven • e-Bay, JetBrains, Sky, Ubisoft, Wix, Angular, Ionic, Aurelia TypeScript – JavaScript that scales
  14. • Block-scoped variables with let and const • Supports inheritance

    and composition patterns (extends and implements) • Public, private and protected accessors • Arrow functions • Generics with constraints • Module system TypeScript – JavaScript that scales
  15. TypeScript – JavaScript that scales interface Person { firstName: string;

    lastName: string; } class Worker implements Person { constructor( public firstName: string, public lastName : string ){} greeting(): void { alert(`Hello, my name is ${this.firstName} ${this.lastName}`); } }
  16. TypeScript – JavaScript that scales npm install –g typescript tsc

    someFile.ts //creates someFile.js tsc –w //watch
  17. TypeScript – JavaScript that scales tsconfig.json– TypeScript compiler configuration {

    "compilerOptions": { "module": "system", "noImplicitAny": true, "removeComments": true, "preserveConstEnums": true, "outFile": "../../built/local/tsc.js", "sourceMap": true }, "include": [ "src/**/*" ], "exclude": [ "node_modules", "**/*.spec.ts" ] }
  18. • Well-designed modern web language • New language features don’t

    ruin your compatibility • IDE tooling (refactoring, navigation, and so on) • Save time for maintenance and refactoring • Just save money Why TypeScript?
  19. • Cross-platform development experience • Optimized for performance • Best

    available tools Angular2 – modern front-end development platform
  20. import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule }

    from './app.module'; platformBrowserDynamic().bootstrapModule(AppModule); import { platformNativeScriptDynamic } from "nativescript-angular/platform"; import { AppModule } from './app.module'; platformNativeScriptDynamic().bootstrapModule(AppModule);
  21. import { NgModule } from '@angular/core'; @NgModule({ imports: [ …

    ], providers: [ … ], declarations: [ … ], exports: [ … ], bootstrap: [ AppComponent ] }) export class AppModule { }
  22. class PersonComponent { firstName: string; lastName: string; greeting(): void {

    alert(`Hello, ${this.firstName} ${this.lastName}!`); } }
  23. @Component({ moduleId: module.id, selector: 'person', styleUrls: [ 'person.component.css' ], templateUrl:

    'person.component.html', providers: [ PeopleService ] }) class PersonComponent { firstName: string; lastName: string; greeting(): void { alert(`Hello, ${this.firstName} ${this.lastName}!`); } }
  24. import { Component } from '@angular/core'; import { PeopleService }

    from './PeopleService'; @Component({ moduleId: module.id, selector: 'person', styleUrls: [ 'person.component.css' ], templateUrl: 'person.component.html', providers: [ PeopleService ] }) class PersonComponent { firstName: string; lastName: string; greeting(): void { alert(`Hello, ${this.firstName} ${this.lastName}!`); } }
  25. ngOnChanges ngOnInit ngOnDestroy and more… import { Component, OnInit }

    '@angular/core'; @Component({ ... }) export class SomeClass implements OnInit { ngOnInit(): void { ... } }
  26. <div *ngIf="someProperty"></div> <div *ngFor="let item of items">{{item}}</div> <span [ngSwitch]="someEnum"> <span

    *ngSwitchCase="'one'">One</span> <span *ngSwitchCase="'two'">Two</span> <span *ngSwitchCase="'three'">Three</span> </div>
  27. Angular CLI • npm install –g angular-cli • ng new

    PROJECT_NAME • ng serve • ng g component component-name • ng g service service-name • and so on… Command line utility for building ng2 apps
  28. Ahead of Time (AOT) compilation • Compiler inlines html templates

    and css stylesheets • Clients don’t have to download compiler code • Template errors can be found during build step • Only needed code is being built (tree shaking) Compile your code during build process
  29. Zones Easier way to control code running outside of Angular

    function TestController() { var ctrl = this; ctrl.firstName = 'Przemek'; window.setTimeout(function() { ctrl.firstName = 'Tomek'; }, 1000); } function TestController($timeout) { var ctrl = this; ctrl.firstName = 'Przemek'; $timeout(function() { ctrl.firstName = 'Tomek'; }, 1000); }
  30. Zones Easier way to control code running outside of Angular

    constructor(zone: NgZone) { const mql: MediaQueryList = window.matchMedia('(min-width: 600px)'); mql.addListener((mql: MediaQueryList) => { zone.run(() => { this.text = 'Screen Width: ' + (mql.matches ? ' GREATER' : 'LOWER'); }); }); } http://blog.assaf.co/angular-2-change-detection-zones-and-an-example/
  31. And more… • Improved DI • Angular Router • Redux

    / Reactive patterns friendly • Angular Material 2 • Angular Testing Utilities • Angular Universal
  32. Lessons learned • Front-end development isn’t only about changing colours

    of the buttons • Keep the right balance between 'why' and 'how' approaches • Angular2 is production ready and it’s pretty damn hot!