└── README.md
/README.md:
--------------------------------------------------------------------------------
1 | # Angular Cheatsheet
2 | Set of basic functionalities from Angular in one place. Thanks to http://foreach.pl for contribute
3 |
4 | **Check also other Cheatsheets:**
5 |
6 | [TypeScript](https://github.com/delprzemo/typescript-cheatsheet)
7 |
8 | [ReactJS](https://github.com/delprzemo/react-cheatsheet)
9 |
10 | # Table of Contents
11 |
12 |
13 |
14 | * [AngularCli](#AngularCLI)
15 | * [Components and Templates](#Components-and-Templates)
16 | * [Sample component ts file](#Sample-component-ts-file)
17 | * [Component life cycles](#Component-life-cycles)
18 | * [Template syntax](#Template-syntax)
19 | * [Input and Output](#Input-and-Output)
20 | * [Content projection](#Content-projection)
21 | * [ViewChild decorator](#ViewChild-decorator)
22 | * [Routing](#Routing)
23 | * [CanActivate and CanDeactivate](#CanActivate-and-CanDeactivate)
24 | * [Modules](#Modules)
25 | * [Services](#Services)
26 | * [HttpClient](#HttpClient)
27 | * [Dependency Injection](#Dependency-Injection)
28 | * [Declare global values](#Declare-global-values)
29 | * [Pipes](#Pipes)
30 | * [Directives](#Directives)
31 | * [Animations](#Animations)
32 | * [Angular Forms](#Angular-Forms)
33 | * [Template driven forms](#Template-driven-forms)
34 | * [Reactive forms](#Reactive-forms)
35 | * [Custom Validator for Reactive forms](#Custom-Validator-for-Reactive-forms)
36 | * [Custom Validator Directive for Template driven forms](#Custom-Validator-Directive-for-Template-driven-forms)
37 | * [ngModel in custom component](#ngModel-in-custom-component)
38 | * [Tests](#Tests)
39 | * [Unit tests](#Unit-tests)
40 | * [Others](#Others)
41 | * [Http interceptor](#Http-interceptor)
42 | * [host](#host)
43 | * [Interview questions](#Interview-questions)
44 |
45 |
46 |
47 | AngularCLI
48 | =================
49 |
50 | Command line inferface for Angular - set of commands that will help us during development.
51 |
52 | **1. Setup**
53 |
54 | | Command | Description |
55 | | ------------- | ------------- |
56 | | npm install -g @angular/cli | Install Angular CLI globally |
57 |
58 | **2. New application**
59 |
60 | | Command | Description |
61 | | ------------- | ------------- |
62 | | ng new best-practises --dry-run | just simulate ng new |
63 | | ng new best-practises --skip-install | skip install means don't run npm install |
64 | | ng new best-practises --prefix best | set prefix to best |
65 | | ng new --help | check available command list |
66 |
67 |
68 | **3. Lint - check and make sure that our code if free of code smells/ bad formatting**
69 |
70 | | Command | Description |
71 | | ------------- | ------------- |
72 | | ng lint my-app --help | check available command list |
73 | | ng lint my-app --format stylish | format code |
74 | | ng lint my-app --fix | fix code smells |
75 | | ng lint my-app | show warnings |
76 |
77 |
78 | **4. Blueprints**
79 |
80 | | Command | Description |
81 | | ------------- | ------------- |
82 | | ng g c my-component --flat true | don't create new folder for this component |
83 | | --inline-template (-t) | will the template be in .ts file? |
84 | | --inline-style (-s) | will the style be in .ts file? |
85 | | --spec | generate spec? |
86 | | --prefix | assign own prefix |
87 | | ng g d directive-name | create directive |
88 | | ng g s service-name | create service |
89 | | ng g cl models/customer | create customer class in models folder |
90 | | ng g i models/person | create create interface in models folder |
91 | | ng g e models/gender | create create ENUM gender in models folder |
92 | | ng g p init-caps | create create pipe |
93 |
94 | **5. Building&Serving**
95 |
96 | | Command | Description |
97 | | ------------- | ------------- |
98 | | ng build | build app to /dist folder |
99 | | ng build --aot | build app without code that we don't need (optimatization) |
100 | | ng build --prod | build for production |
101 | | ng serve -o | serve with opening a browser |
102 | | ng serve --live-reload | reload when changes occur |
103 | | ng serve -ssl | serving using SSL |
104 |
105 | **6. Add new capabilities**
106 |
107 | | Command | Description |
108 | | ------------- | ------------- |
109 | | ng add @angular/material | add angular material to project |
110 | | ng g @angular/material:material-nav --name nav | create material navigation component |
111 |
112 | # Components and Templates
113 |
114 | Components are the most basic UI building block of an Angular app. An Angular app contains a tree of Angular components.
115 |
116 | ## Sample component ts file
117 | ```ts
118 | import { Component } from '@angular/core';
119 |
120 | @Component({
121 | // component attributes
122 | selector: 'app-root',
123 | templateUrl: './app.component.html',
124 | styleUrls: ['./app.component.less']
125 | })
126 |
127 | export class AppComponent {
128 | title = 'my-dogs-training';
129 | }
130 | ```
131 |
132 | ## Component attributes
133 |
134 |
135 | | Attribute | Description |
136 | | ------------- | ------------- |
137 | | changeDetection | The change-detection strategy to use for this component. |
138 | | viewProviders | Defines the set of injectable objects that are visible to its view DOM children |
139 | | moduleId | The module ID of the module that contains the component |
140 | | encapsulation | An encapsulation policy for the template and CSS styles |
141 | | interpolation | Overrides the default encapsulation start and end delimiters ({{ and }} |
142 | | entryComponents | A set of components that should be compiled along with this component. |
143 | | preserveWhitespaces | True to preserve or false to remove potentially superfluous whitespace characters from the compiled template. |
144 |
145 | ## Component life cycles
146 |
147 | | Life cycle | Description |
148 | | ------------- | ------------- |
149 | | ngOnInit | Called once, after the first ngOnChanges() |
150 | | ngOnChanges | Called before ngOnInit() and whenever one of input properties change. |
151 | | ngOnDestroy | Called just before Angular destroys the directive/component |
152 | | ngDoCheck | Called during every change detection run |
153 | | ngAfterContentChecked | Called after the ngAfterContentInit() and every subsequent ngDoCheck() |
154 | | ngAfterViewChecked | Called after the ngAfterViewInit() and every subsequent ngAfterContentChecked(). |
155 | | ngAfterContentInit | Called once after the first ngDoCheck(). |
156 | | ngAfterViewInit | Called once after the first ngAfterContentChecked(). |
157 |
158 | ## Template syntax
159 |
160 | | Syntax | Description |
161 | | ------------- | ------------- |
162 | | {{user.name}} | Interpolation - just generate user name here |
163 | | | property binding - bind image url for user to src attribute |
164 | | | Event - assign function to click event |
165 | | | Show button when user.showSth is true |
166 | | *ngFor="let item of items" | Iterate through items list |
167 | |
Highlight me!
516 | ``` 517 | 518 | 519 | # Animations 520 | Animations - moving from style state to another style state. Before add BrowserModule and BrowserAnimationsModule to module 521 | 522 | **Implementation:** 523 | ```ts 524 | animations: [ 525 | trigger('openClose', [ 526 | state('open', style({ 527 | height: '400px', 528 | opacity: 1.5, 529 | })), 530 | state('closed', style({ 531 | height: '100px', 532 | opacity: 0.5, 533 | })), 534 | transition('open => closed', [ 535 | animate('1s') 536 | ]), 537 | transition('closed => open', [ 538 | animate('1s') 539 | ]) 540 | ]) 541 | ] 542 | ``` 543 | 544 | **usage** 545 | ```html 546 |