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

Angular 2 fundamentals

Angular 2 fundamentals

Angular 1 helped us to build amazing apps with ease... but today's web apps are facing new challenges, going even beyond web platform.

How to solve them you may ask... Well let me tell you that there is a new Angular. Angular 2 at your service...

Martin will tell you all you need to know to get started with this new and shinny multi-platform framework

Martin Hochel

March 30, 2016
Tweet

More Decks by Martin Hochel

Other Decks in Programming

Transcript

  1. Angular 2 comes with - HTML Parser - Dependency Injection

    - Router - Forms (data/template driven) - HTTP - Animations - I18N and A11Y - Testing
  2. class TalkComponent { talk: Talk; rate: EventEmitter = new EventEmitter();

    } talk.componet.ts @Component({ selector: 'talk', templateUrl: 'talk.html', directives: [FormattedRating, WatchButton, RateButton] })
  3. ES.next decorators class C { @enumerable(false) method() { } }

    function enumerable(value) { return function (target, key, descriptor) { descriptor.enumerable = value; } }
  4. class TalkComponent { talk: Talk; rate: EventEmitter = new EventEmitter();

    } talk.componet.ts @Component({ selector: 'talk', templateUrl: 'talk.html', directives: [FormattedRating, WatchButton, RateButton] })
  5. {{ talk.title }} {{ talk.speaker }} <formatted-rating [rating]=“talk.rating" ></formatted-rating> <watch-button

    [talk]="talk" ></watch-button> <rate-button [talk]="talk" ></rate-button> talk.html class TalkComponent { talk: Talk; rate: EventEmitter = new EventEmitter(); } @Component({ selector: 'talk', templateUrl: 'talk.html', directives: [ FormattedRating, WatchButton, RateButton ] })
  6. talk.componet.ts @Component({ selector: 'talk', templateUrl: 'talk.component.html', directives: [FormattedRating, WatchButton, RateButton]

    }) class TalkComponent { talk: Talk; rate: EventEmitter = new EventEmitter(); } @Input() @Output()
  7. @Component({ selector: 'talk', templateUrl: 'talk.html' }) class TalkComponent implements AfterViewInit

    { @ViewChild(ChildCmp) child; ngAfterViewInit () { // do something with child } } Component Lifecycle
  8. @Directive({ selector: '[trimmed-input]' }) class TrimmedInput { } @HostListener('input', ['$event.target.value'])

    onValueUpdate(updatedValue: string) { this.value = updatedValue.trim(); } Components can interact with their host <input type="text" trimmed-input> value @HostBinding() value: string;
  9. Components are self-describing • Knows how to interact with the

    host element • Knows how to render itself • Configures its dependencies • Isolated / Modular
  10. Components API • Component definition • Inputs / Outputs •

    Lifecycle hooks • Host element interaction
  11. // talks-app-backend.service.ts class TalksAppBackend { fetchTalks() { return [ {

    name: 'Are we there yet?' }, { name: 'The value of values' } ]; } } Angular 2 Service
  12. // TalkList.ts class TalkListComponent { constructor() { this.backend = new

    TalksAppBackend(); this.talks = this.backend.fetchTalks(); } } Provide the service?
  13. // talk-list.component.ts @Component({ selector: 'talk-list', templateUrl: 'talk-list.html' }) class TalkListComponent

    implements OnInit { constructor(private backend: TalksAppBackend) {} ngOnInit() { this.talks = backend.fetchTalks(); } } Dependency Injection
  14. // TalkList.ts @Component({selector: 'talk-list'}) class TalkList implements OnInit { constructor(private

    backend: TalksAppBackend) {} ngOnInit() { this.talks = backend.fetchTalks(); } } Dependency Injection // app.ts @Component({ selector: 'talk-app', providers: [TalksAppBackend] }) class Application {}
  15. • Decoupled Code • Testability • Single DI API •

    Hierarchical DI Dependency Injection
  16. const appState = { filters: { speaker: "Rich Hickey" },

    talks: [ { title: "Are We There Yet?", speaker: "Rich Hickey", yourRating: null, rating: 9.1 } ] } 9.9 9.9
  17. Wanna know more? • angular.io • Thoughtram blog • Angular

    2 - High Voltage collection • http://victorsavkin.com • egghead.io