Repository: delprzemo/angular-cheatsheet
Branch: master
Commit: 19e665e31da1
Files: 1
Total size: 29.1 KB
Directory structure:
gitextract_vr8qfp27/
└── README.md
================================================
FILE CONTENTS
================================================
================================================
FILE: README.md
================================================
# Angular Cheatsheet
Set of basic functionalities from Angular in one place. Thanks to http://foreach.pl for contribute
**Check also other Cheatsheets:**
[TypeScript](https://github.com/delprzemo/typescript-cheatsheet)
[ReactJS](https://github.com/delprzemo/react-cheatsheet)
# Table of Contents
* [AngularCli](#AngularCLI)
* [Components and Templates](#Components-and-Templates)
* [Sample component ts file](#Sample-component-ts-file)
* [Component life cycles](#Component-life-cycles)
* [Template syntax](#Template-syntax)
* [Input and Output](#Input-and-Output)
* [Content projection](#Content-projection)
* [ViewChild decorator](#ViewChild-decorator)
* [Routing](#Routing)
* [CanActivate and CanDeactivate](#CanActivate-and-CanDeactivate)
* [Modules](#Modules)
* [Services](#Services)
* [HttpClient](#HttpClient)
* [Dependency Injection](#Dependency-Injection)
* [Declare global values](#Declare-global-values)
* [Pipes](#Pipes)
* [Directives](#Directives)
* [Animations](#Animations)
* [Angular Forms](#Angular-Forms)
* [Template driven forms](#Template-driven-forms)
* [Reactive forms](#Reactive-forms)
* [Custom Validator for Reactive forms](#Custom-Validator-for-Reactive-forms)
* [Custom Validator Directive for Template driven forms](#Custom-Validator-Directive-for-Template-driven-forms)
* [ngModel in custom component](#ngModel-in-custom-component)
* [Tests](#Tests)
* [Unit tests](#Unit-tests)
* [Others](#Others)
* [Http interceptor](#Http-interceptor)
* [host](#host)
* [Interview questions](#Interview-questions)
AngularCLI
=================
Command line inferface for Angular - set of commands that will help us during development.
**1. Setup**
| Command | Description |
| ------------- | ------------- |
| npm install -g @angular/cli | Install Angular CLI globally |
**2. New application**
| Command | Description |
| ------------- | ------------- |
| ng new best-practises --dry-run | just simulate ng new |
| ng new best-practises --skip-install | skip install means don't run npm install |
| ng new best-practises --prefix best | set prefix to best |
| ng new --help | check available command list |
**3. Lint - check and make sure that our code if free of code smells/ bad formatting**
| Command | Description |
| ------------- | ------------- |
| ng lint my-app --help | check available command list |
| ng lint my-app --format stylish | format code |
| ng lint my-app --fix | fix code smells |
| ng lint my-app | show warnings |
**4. Blueprints**
| Command | Description |
| ------------- | ------------- |
| ng g c my-component --flat true | don't create new folder for this component |
| --inline-template (-t) | will the template be in .ts file? |
| --inline-style (-s) | will the style be in .ts file? |
| --spec | generate spec? |
| --prefix | assign own prefix |
| ng g d directive-name | create directive |
| ng g s service-name | create service |
| ng g cl models/customer | create customer class in models folder |
| ng g i models/person | create create interface in models folder |
| ng g e models/gender | create create ENUM gender in models folder |
| ng g p init-caps | create create pipe |
**5. Building&Serving**
| Command | Description |
| ------------- | ------------- |
| ng build | build app to /dist folder |
| ng build --aot | build app without code that we don't need (optimatization) |
| ng build --prod | build for production |
| ng serve -o | serve with opening a browser |
| ng serve --live-reload | reload when changes occur |
| ng serve -ssl | serving using SSL |
**6. Add new capabilities**
| Command | Description |
| ------------- | ------------- |
| ng add @angular/material | add angular material to project |
| ng g @angular/material:material-nav --name nav | create material navigation component |
# Components and Templates
Components are the most basic UI building block of an Angular app. An Angular app contains a tree of Angular components.
## Sample component ts file
```ts
import { Component } from '@angular/core';
@Component({
// component attributes
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.less']
})
export class AppComponent {
title = 'my-dogs-training';
}
```
## Component attributes
| Attribute | Description |
| ------------- | ------------- |
| changeDetection | The change-detection strategy to use for this component. |
| viewProviders | Defines the set of injectable objects that are visible to its view DOM children |
| moduleId | The module ID of the module that contains the component |
| encapsulation | An encapsulation policy for the template and CSS styles |
| interpolation | Overrides the default encapsulation start and end delimiters ({{ and }} |
| entryComponents | A set of components that should be compiled along with this component. |
| preserveWhitespaces | True to preserve or false to remove potentially superfluous whitespace characters from the compiled template. |
## Component life cycles
| Life cycle | Description |
| ------------- | ------------- |
| ngOnInit | Called once, after the first ngOnChanges() |
| ngOnChanges | Called before ngOnInit() and whenever one of input properties change. |
| ngOnDestroy | Called just before Angular destroys the directive/component |
| ngDoCheck | Called during every change detection run |
| ngAfterContentChecked | Called after the ngAfterContentInit() and every subsequent ngDoCheck() |
| ngAfterViewChecked | Called after the ngAfterViewInit() and every subsequent ngAfterContentChecked(). |
| ngAfterContentInit | Called once after the first ngDoCheck(). |
| ngAfterViewInit | Called once after the first ngAfterContentChecked(). |
## Template syntax
| Syntax | Description |
| ------------- | ------------- |
| {{user.name}} | Interpolation - just generate user name here |
| | property binding - bind image url for user to src attribute |
| | Event - assign function to click event |
| | Show button when user.showSth is true |
| *ngFor="let item of items" | Iterate through items list |
|
Highlight me!
``` # Animations Animations - moving from style state to another style state. Before add BrowserModule and BrowserAnimationsModule to module **Implementation:** ```ts animations: [ trigger('openClose', [ state('open', style({ height: '400px', opacity: 1.5, })), state('closed', style({ height: '100px', opacity: 0.5, })), transition('open => closed', [ animate('1s') ]), transition('closed => open', [ animate('1s') ]) ]) ] ``` **usage** ```html