Angular 2 Notes

Found these lying around my drive while cleaning, not sure how old/what versionthey are for.

  • Structural directives change the dom, denotes with *
  • binding
    • interpolaion :
    • 1 way : <div [style.color]="color"></div>
    • event : (click)="log('click')
    • 2 way : <input \[(ngModel)]="story.name"> // banana in a box.
  • Services/Factories are replaced with declared classes
  • DI
    • register in @component.providers arr
      • each provide is a new instance, ie top level only
    • inject in the COmponent class constructor
  • There are angular 2 chaet sheet on the developer guider
  • Component has 3 pieces
    1. imprts
    2. metadata @Component -selector,template,systelsUrls,directives,providers
    3. export class <name implements <interface> {
      • @Output, @Input, constructor, properties, actions (methods)
      • ParentComponents can declare @ViewChild to acces child components.
        • @ViewChild(ChildCanFilterComponent) thisPropNameOnParent: ChildCanFilterComponent
          • ChildCanFilter must be declared in the metadata directives list
  • File Naming <name>.<type>.<ext> - character.service.ts
  • Models can be put in services and imported from there.
  • Services get imported to the app.compoent in it's metadata providers property
    • ~@Injectable~compies the type information
    • Only required in it is being injected into, but add it for good habit
  • DI is hieracical in ng2, soe register stuff at the parent of all who need it.
  • Registering DI twice will give 2 instances
  • Component Lifecycle hooks
    • When stuff is created/destroyed
    • trigger event with implements OnInit /interface syntax
      • method is actually ngOnInit
      • use onInit to initalizaed data instead of the constructor
      • common: ngOnChanges, ngOnInit, ngAfterViewInit, ngOnChanges, gnOnDestroy
  • http module has to be included separatly
  • Observables returns observables.
    • Observables implement subscribe (not then) which then handle the success/fail cases.
    • Observable is in RxJs
    • A few RxJs methods _http.get().map().do().catch(). Docs have more. reactivex.io
    • Prod build only includes the RxJs othat is imported, dev is everything
  • | async pipe allows sending an Observable tothe screen
  • Routing
    • Routing to a link with [routerlink]="link
    • Steps
      1. ROUTEPROVIDERS
      2. @RouteConfig
      3. <router-outlet>
      4. [routerLink]
    • RouteConfig name must be PascalCase
    • using router must include RouterDirective in @Ccomponent directovets
    • /vechicles/... indicates a child route to be defined in the component included
    • RouteParams contains the ussual query string and path
  • General order of adding stuff
    1. Add to metadata (providers) - @Component(ROUTEPROVIDERS)
    2. Add to imports -from 'angular2/router'
    3. Add more metadata (@RouteConfig), then more import in this case RouteConfig
    4. Add to class
  • Any directive in html, eg anything in [], must be imported. Don't forget the Angular ones like [routerLink]
  • Any service in constructor must be imported