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.
- interpolaion :
- 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
- register in @component.providers arr
- There are angular 2 chaet sheet on the developer guider
- Component has 3 pieces
- imprts
- metadata
@Component
-selector,template,systelsUrls,directives,providers - 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
- ChildCanFilter must be declared in the metadata
- 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
- method is actually
- 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
- ROUTEPROVIDERS
- @RouteConfig
- <router-outlet>
- [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
- Routing to a link with
- General order of adding stuff
- Add to metadata (providers) - @Component(ROUTEPROVIDERS)
- Add to imports -from 'angular2/router'
- Add more metadata (@RouteConfig), then more import in this case RouteConfig
- 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