Show Menu
Cheatography

Angular Structures Cheat Sheet (DRAFT) by

Things that might be nice to know when setting up an Angular project.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Project setup

Keep things organized in folders (Modules are not the default as of 2024). All pages, compon­ents, routes, valida­tors, services that belong together should be have the same (grand­)pa­rent.

Project setup commands - sample

# When setting up a new project think about how you want it to look. Make a short list of commands to set up your project. Open the new project in your IDE, if things do not feel right, adjust your list and run it again. I have added an example below.

ng new sample

cd sample
ng g c core/homepage/containers/homepage

ng g c core/products/containers/products
ng g c core/products/containers/product-edit
ng g c core/products/containers/product-edit
ng g c core/products/containers/product-add
ng g c core/products/components/product-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­anges()
Respond when Angular (re)sets data-bound input proper­ties. The method receives a Simple­Changes object of current and previous property values. Called before ngOnInit() and whenever one or more data-bound input properties change.
ngOnInit()
Initialize the direct­ive­/co­mponent after Angular first displays the data-bound properties and sets the direct­ive­/co­mpo­nent's input proper­ties. Called once, after the first ngOnCh­ang­es().
ngDoCh­eck()
Detect and act upon changes that Angular can't or won't detect on its own. Called during every change detection run, immedi­ately after ngOnCh­anges() and ngOnIn­it().
ngAfte­rCo­nte­ntI­nit()
Respond after Angular projects external content into the compon­ent's view / the view that a directive is in. Called once after the first ngDoCh­eck().
ngAfte­rVi­ewI­nit()
Respond after Angular initia­lizes the compon­ent's views and child views / the view that a directive is in. Called once after the first ngAfte­rCo­nte­ntC­hec­ked().
ngAfterViewChecked()
Respond after Angular checks the compon­ent's views and child views / the view that a directive is in. Called after the ngAfte­rVi­ewI­nit() and every subsequent ngAfte­rCo­nte­ntC­hec­ked().
ngOnDe­stroy()
Cleanup just before Angular destroys the direct­ive­/co­mpo­nent. Unsubs­cribe Observ­ables and detach event handlers to avoid memory leaks. Called just before Angular destroys the direct­ive­/co­mpo­nent.