Angular Structures Cheat Sheet
by Robert Broen (rbroen) via cheatography.com/64564/cs/20562/
Project setup Life cycle hooks (cont)
Keep things organized in folders (Modules are not the default as of ngDoCheck() Detect and act upon changes that Angular
2024). All pages, components, routes, validators, services that can't or won't detect on its own. Called
belong together should be have the same (grand)parent. during every change detection run,
immediately after ngOnChanges() and
Project setup commands - sample ngOnInit().
# When setting up a new project think about how ngAfterContentInit() Respond after Angular projects external
you want it to look. Make a short list of commands content into the component's view / the
to set up your project. Open the new project in view that a directive is in. Called once after
your IDE, if things do not feel right, adjust your the first ngDoCheck().
list and run it again. I have added an example ngAfterViewInit() Respond after Angular initializes the
below. component's views and child views / the
ng new sample view that a directive is in. Called once after
cd sample the first ngAfterContentChecked().
ng g c core/homepage/containers/homepage ngAfterViewChecked() Respond after Angular checks the compon‐
ng g c core/products/containers/products ent's views and child views / the view that a
ng g c core/products/containers/product- directive is in. Called after the ngAfterVi‐
edit ewInit() and every subsequent ngAfterCo‐
ntentChecked().
ng g c core/products/containers/product-
edit ngOnDestroy() Cleanup just before Angular destroys the
ng g c core/products/containers/product- directive/component. Unsubscribe Observ‐
add ables and detach event handlers to avoid
memory leaks. Called just before Angular
ng g c core/products/components/product-
destroys the directive/component.
form
ng g s core/products/services/product
ng g c core/contact/containers/contact
# Make many more so you get a good feel of how your
decisions will impact the project. Make changes,
delete the project and run you commands again.
# Add a route to the ProductsComponent at
/products
# Use the ProductService in the constructor of
the ProductsComponent
Life cycle hooks
ngOnCh‐ Respond when Angular (re)sets data-bound input
anges() properties. The method receives a SimpleChanges
object of current and previous property values. Called
before ngOnInit() and whenever one or more data-
bound input properties change.
ngOnInit() Initialize the directive/component after Angular first
displays the data-bound properties and sets the direct‐
ive/component's input properties. Called once, after the
first ngOnChanges().
By Robert Broen (rbroen) Not published yet. Sponsored by CrosswordCheats.com
cheatography.com/rbroen/ Last updated 5th August, 2024. Learn to solve cryptic crosswords!
Page 1 of 1. http://crosswordcheats.com