Cheat Sheet - Components & Databinding: Component Metadata
Cheat Sheet - Components & Databinding: Component Metadata
Cheat Sheet - Components & Databinding: Component Metadata
Databinding
Component Metadata
Components are normal TypeScript/ JS classes, transformed into
components by adding Metadata.
@Component({
selector: my-selector,
template: `
<h1>A heading!</h1>
`
})
The @Component decorator (which applies the metadata) allows the
application of a variety of different metadata configurations.
The template metadata is always required, since that makes up a component.
Often times (always if not using routing), youll also need the selector
metadata.
For a complete overview over the available metadata which you may add
inside the @Component decorator, visit this link:
https://angular.io/docs/ts/latest/api/core/index/ComponentMetadata-
class.html
NgModule
NgModule is a concept introduced with RC5, which allows you to modularize
your Application whilst saving boilerplate code.
You can find a detailed guide here:
https://angular.io/docs/ts/latest/guide/ngmodule.html
Upgrade Notes (RC4 to RC5) can be found here:
https://angular.io/docs/ts/latest/cookbook/rc4-to-rc5.html
Databinding
Databinding allows you to communicate between the component/ class body
and the template, as well as between different parts of your application.
Angular 2 knows four forms of Databinding:
1. String interpolation
{{property_resolving_to_string}}
2. Property Binding
<img [src]=img_src_path>
3. Event Binding
<button (click)=onClick()>
4. Two-Way Binding
<input type=text [(ngModel)]=myModel.name>
Also consult the official Angular 2 Cheat Sheet, which goes into more detail
about the different syntaxes etc: https://angular.io/cheatsheet
Local References
Inside templates you may create local references like this:
<div #myDiv>
This will create a reference to the DIV HTML element which you may use
throughout the template (NOT inside the component class body!).
If you want to get a reference to this element in your component class body,
get it by using @ViewChild, like this:
@ViewChild(myDiv) referenceToDivElement;
Component Lifecycle
Angular 2 Components follow a lifecycle when created (which is taken care of
by Angular 2).
Consult this link for more information on the different hooks and when they
are reached: https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html