-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAngular.txt
More file actions
400 lines (206 loc) · 12.1 KB
/
Angular.txt
File metadata and controls
400 lines (206 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
* https://nodejs.org/en/
* https://angular.io/
* https://cli.angular.io/
* Angular JS is initially released in 2010
* Angular 2 is released in 2016
* Angular 2 is completely re-write of Angular JS
* ng new "appName"
npm install
ng serve --open
ng generate component [componentName]
ng generate service [serviceName]
https://hype.codes/differences-between-promise-and-observable-angular
* Promise
A Promise handles a single event when an async operation completes or fails.
Note: There are Promise libraries out there that support cancellation, but ES6 Promise doesn't so far.
* Observable
An Observable is like a Stream (in many languages) and allows to pass zero or more events where the callback is called for each event.
Often Observable is preferred over Promise because it provides the features of Promise and more. With Observable it doesn't matter if you want to handle 0, 1, or multiple events. You can utilize the same API in each case.
Observable also has the advantage over Promise to be cancelable. If the result of an HTTP request to a server or some other expensive async operation isn't needed anymore, the Subscription of an Observable allows to cancel the subscription, while a Promise will eventually call the success or failed callback even when you don't need the notification or the result it provides anymore.
Observable provides operators like map, forEach, reduce, ... similar to an array
There are also powerful operators like retry(), or replay(), ... that are often quite handy.
*********************************************************************************
some meta data is called "Component".
* Service is a class with a specific purpose.
* The response that we get from http service is called "Observable".
* Angular application is a collection of many invidual modules.
* Every Angular application has at least one module that is route module - AppModule by convension
* Every Angular application has at least one component that is route component - AppComponent by convension
* A component controls a portion of the view on the browser.
* All components will be nested in route component.
* Each component will have a html template to represent the view in the browser and a class that controls the logic of that particular view.
* Module contains services that contains business logic of the application
* In Angular app main file is "main.ts" files it resides inside of "src" folder.
* Angular cli is used to create building blocks.
* A component is made up of 3 things, 1)Template 2)Class 3)Meta data
* There are 3 ways to specify the selector - 1)app-test 2).app-test 3)[app-test]
* A property within double curly braces is known as "Interpolation" in Angular
* Interpolation only works with Strings but not boolean things.
To work with boolean things "Property binding" comes to the picture.
Ex: <input type='text' [disabled]='isDisabled' value='Manoj' id={{myId}}' />
Ex: <input type='text' bind-disabled='isDisabled' value='Manoj' id={{myId}}' />
let myId = 'idName';
let isDisabled = true;
https://www.youtube.com/watch?v=N8FBmB2jme8&list=PLC3y8-rFHvwhBRAgFinJR8KHIrCdTkZcZ&index=6
* A directive is nothing but a custom attribute that Angular provides.
* Class binding & style binding & event binding
* Template refence variables = #attribute
Ex: <input type='text' value='' #inputField />
<button (click)="logMsg($event)">Click</button>
<button (click)="logMsg(inputField)">Click</button>
<button (click)="logMsg(inputField.value)">Click</button>
* Two way binding - We have to use [(ngModel)]="name"
To use "ngModel", we have import "FormsModule" into app.module.ts file
* Structural directives
------------------------------
* Structural directives allows us to add or remove html elements.
* ngIf, ngSwitch and ngFor
* ngIf and ngSwitch are used to conditionally render HTML elements
* ngFor is used to render list of HTML elements.
* ngIf
----------------
Ex1:
<div *ngIf="false; else elseBlock">This is statement 1</div>
<ng-template #elseBlock>This is statement 2</div>
Ex2:
<div *ngIf="false; then thenBlock; else elseBlock"></div>
<ng-template #thenBlock>This is statement 1</div>
<ng-template #elseBlock>This is statement 2</div>
* ngSwitch:
-------------------------
<div [ngSwitch]="color">
<div *ngSwitchCase="white">You picked white color</div>
<div *ngSwitchCase="red">You picked red color</div>
<div *ngSwitchCase="black">You picked black color</div>
<div *ngSwitchDefault="black">You picked no color</div>
</div>
* ngFor:
--------------------
<div *ngFor="let color of colors">
<h1>{{ color }}</h1>
</div>
<div *ngFor="let color of colors; index as i">
<h1>{{i}} {{ color }}</h1>
</div>
<div *ngFor="let color of colors; first as f">
<h1>{{f}} {{ color }}</h1>
</div>
<div *ngFor="let color of colors; last as l">
<h1>{{l}} {{ color }}</h1>
</div>
<div *ngFor="let color of colors; odd as o">
<h1>{{o}} {{ color }}</h1>
</div>
<div *ngFor="let color of colors; even as e">
<h1>{{e}} {{ color }}</h1>
</div>
* Component Interaction - @input & @output
* Using @input decorator child component can accept input from parent component
* Using @output decorator child component can send events to parent component
* <app-test (childEvent)="message=$event" [parentData]="name"></app-test>
* Pipes
------------------------
* String pipes
* Number pipes
* json pipes
* Date pipes
* currency pipes
* Service:
----------------------------------
* A Service is a class with a specific purpose
* We can share the data to multiple components
* Implement application logic
* External interaction like connecting to database - HTTP requests
* Naming convension is - .service.ts
* Dependency Injection (DI):
----------------------------------
* DI is a coding pattern in which a class receives its dependencies from external sources rather than creating them itself.
* DI framework has something called Injector where you register all your dependencies.
* The ngOnInit hook gets called once the component is initialized.
HTTP & Observable:
--------------------------------
* The response we get from HTTP call is an Observable.
* Observable is nothing but a sequence of items that arrive asynchronously over time.
* First we will make a HTTP request to server and we receive the observable and cast it to an array and then subscribe to the observable and then assign that array to a local variable.
RxJS: Reactive Extensions for Javascript. External library to work with Observable.
*
https://www.youtube.com/watch?v=qh5nHv-4aw0&list=PLC3y8-rFHvwhBRAgFinJR8KHIrCdTkZcZ&index=25
* 'Transpiling' is the process of converting the typescript into javascript
* Components break up the application into smaller parts; whereas, Directives add behavior to an existing DOM element.
* When it comes to the communication of Angular Components, which are in Parent-Child Relationship; we use @Input in Child Component when we are passing data from Parent to Child Component and @Output is used in Child Component to receive an event from Child to Parent Component.
* Services help us in not repeating the code. With the creation of services, we can use the same code from different components. Here is the command to create a service in angular, ng g service User (a UserService is created when this command is used).
* When a component is dependent on another component the dependency is injected/provided during runtime.
* Routing helps a user in navigating to different pages using links.
* A web page in Angular has many components involved in it. A Component is basically a block in which the data can be displayed on HTML using some logic usually written in typescript.
* ngModel is a directive which can be applied on a text field. This is a two-way data binding. ngModel is represented by [()]
* It is a method which is subscribed to an observable. Whenever the subscribe method is called, an independent execution of the observable happens.
* Observables are lazy, which means nothing happens until a subscription is made. Whereas Promises are eager; which means as soon as a promise is created, the execution takes place. Observable is a stream in which passing of zero or more events is possible and the callback is called for each event. Whereas, promise handles a single event.
* Authentication: The user login credentials are passed to an authenticate API (on the server). On the server side validation of the credentials happens and a JSON Web Token (JWT) is returned. JWT is a JSON object that has some information or attributes about the current user. Once the JWT is given to the client, the client or the user will be identified with that JWT.
Authorization: After logging in successfully, the authenticated or genuine user does not have access to everything. The user is not authorized to access someone else’s data, he/she is authorized to access some data.
*
Routing:
-----------------
* To use routes, first we need to import RouterModule and Routes...
import {RouterModule, Router} from '@angular/router';
Then we should add it to Imports array
RouterModule.forRoot(appRoutes)
Then need to create appRoutes object
const appRoutes: Routes = [
{ path:'', component: HomeComponent },
{ path:'profile', component: ProfileComponent },
{ path:'*', component: HomeComponent }
];
We should add 'base' tag in the body.
Then we should add <router-outlet></router-outlet> tag to render the component.
<li [routerLinkActive]=["active"] [routerLinkActiveOptions]="{exact:true}">
<a [routerLink]=["/login"]>Login</a>
</li>
To make a redirection-
this.router.navigate(['/login']);
*************************************************************************************
Angular by MAX:
------------------------------
* Angular is a Javascript Framework which allows you to create reactive Single-Page-Application
* Whenver we run 'ng serve' command it will create javascript script bundles and those bundles will be added to index.html file.
Bundles names:
inline.bundle.js,
polyfils.bundle.js,
styles.bundle.js,
vendor.bundle.js,
main.bundle.js
* main.ts file is the file which runs first in Angular, it exists inside of 'src' folder.
* @Component, @Injectable, @NgModule are decorators.
* @Component contains below properties
selector, template/templateUrl, styles/styleUrls
* @NgModule contains below properties
declarations, imports, providers, bootsrtap
* Generally we import below packages in app.moddule.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
*************************************************************************************
*************************************************************************************
IMP points to remember:
------------------------------
* Whenver we create any component, we should import it to app.service.ts file and need to add it to "declarations" array
* Whenver we create any service, we should import it to app.service.ts file and need to add it to "Providers" array
* After importing any service, we need to inject it as a dependency into the constructor parameter.
* If we import any modules like 'FormsModule', it should be added to add it to "Imports" array
* Every component includes a class and properties and methods attached to it.
* 'OnInit' is an interface
*************************************************************************************
*************************************************************************************
Tests:
------------------------------
* Unit testing
* Integration test
* End-to-end test
TDD = Test driven development
*************************************************************************************
*************************************************************************************
Angular 2+ Interview questions
https://www.tutorialspoint.com/angular2/angular2_interview_questions.htm
https://www.onlineinterviewquestions.com/angular2-interview-questions/
https://www.onlineinterviewquestions.com/angularjs-interview-questions/
https://www.greycampus.com/blog/programming/top-30-interview-questions-and-answers-on-angular-5