-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangular_help.txt
522 lines (277 loc) · 10.5 KB
/
angular_help.txt
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
Features of Angular
====================
1.Dependency Injection
2.Two way data binding
3.MVC :
With angular we can develop application in clean MVC way
4.Testing : Testing is the area where Angular shines really
5.Modules,Directives,Filters/Pipes,Services, Controllers (Components and Templates) etc....
Module is a container for services,directives,pipes,
components and some other modules also.
AngularJS (1.6) Angular (9.0)
1. Not developed by keeping 1.Developed specially for
mobile devlopment in mind. Mobile development
2. Basic building block is 2.Basic building block is
Java Script Function a TypeScript class
3.Controllers 3.Components and templates
4.filters (sort,format,search) 4.pipes
5.services 5.providers
6.directives 6.directive
ng-if *ngIf
ng-repeat *ngFor ,ngModel
7.routing (function based) 7.routing (JSON based)
8.development is easy 8.complicated
To make angular development easy Angular team has provided one tool (@angular/cli)
@angular/cli tool internally depends on Node JS
1.
Install Node JS : https://nodejs.org/en/download/
Install Visual Studdion Code : https://code.visualstudio.com/
2.node -v
3.npm -v
4.Install angular cli
A. Local Installation
==========================
npm i @angular/cli <enter>
Set Path to Node Js and Angular CLI
C:\Users\PC5037971>set path=c:\node-v7.10.0-win-x64;c:\angular-cli\node_modules\.bin;
B. Global Installation
===========================
npm i -g @angular/cli <enter>
To see the version of angular-cli tool
=======================================
ng version
Create A New Angular Project
===========================
ng new <project_name> <enter>
To strat the application
=========================
Go inside the project on command prompt
npm start => ng serve (from package.json)
angular cli commands
=========================
To create a component => ng g c <Angular4Basics>
To create a service => ng g s <AngularService>
To create a directive => ng g d <AngularDirective>
To create a pipe => ng g p <AngularPipe>
To create a module => ng g m <AngularModule>
To create a class => ng g class <AngularClass>
Routing
===========
/basics => AngularBasics
/pipes => AngularPipes
/technologies => Technologies
templateUrl: './app.component.html',//Where to display if code is greater than 3 lines
template: '<h2>{{title}}</h2>',//Where to display if code is leass than 4 lines
Component is responsible for 4 things
1.What to display?
2.How to display?
3.Where to display?
4.Where to inject?
Angular Binding types
1. Interpolation
Interpolation use the {{ expression }} to render the bound value to the component’s template.
2.Property Binding
Property binding user [] to send values from the component to the template.
3.Event Binding
(click)="showHide()"
4.One way data binding
[ngModel]="name"
4.Two way data binding
[(ngModel)]="name"
Note :TypeScript class members are public by default.
comopnent ->@Component
module ->@NgModule
pipe ->@Pipe
directive ->@Directive
service ->@Injectable
Angular Directives :add functionalities to HTML elements
===========================================
Attribute Directve :Won't change the HTML DOM structure
======================
[ngStyle]
[ngClass]
[ngModel] =>It is applicable for only input elements > FormsModule
[ngSwitch]
Structural Directive :Will change the HTML DOM structure
====================
*ngFor
*ngIf
*ngSwitchCase => It is applicable for only container tags
*ngSwitchDefault=> It is applicable for only container tags
Component Directive
==================
<app-root> </app-root>
<ng-template></ng-template>
2.Custom Directives
=======================
Attribute Directive : ElementRef and Renderer2
====================
<h1 [bgColor]="'red'" [fgColor]="'red'" >Hello World</h1>
Structural Directive :TemplateRef and VieContainerRef
=======================
<h1 *show="true">Hello World</h1>
<h1 *hide="true">Hello World</h1>
Day2 :
======
Angular Pipes & Services
Angular Pipes are used to format ,sort search (filter) the data.
Angular Buitlin Pipes
======================
String
========
uppercase
lowercase
titlecase (angular4)
slice
array
=====
slice
array|slice:startIndex:lastIndex
number
======
number salary|number:'9.3-4'
currency salary|currency:'Rs'
percent salary|percent
date
=====
date
dob|date:'shortDate'
dob|date:'mediumDate'
dob|date:'longDate'
dob|date:'fullDate'
dob|date:'dd-MM-yyyy'
Object
======
object|json
object|keyvalue
Observable
===========
async
Angular Custom Pipes
=====================
gender|gender
gender|gender1 =>1 =Mr. 2=>Ms. 3.
employees|orderBy =>sort employees on id basis in ascending order
employees|orderBy:'id' =>sort employees on id basis in ascending order
employees|orderBy:'name' =>sort employees on name basis in ascending order
employees|orderBy:'name':true =>sort employees on name basis in descending order
employees|filter:'searchText'
Angular Application to perform
CRUD
array.sort((e1,e2)=>e1.id-e2.id);
=>
array.sort(function(e1,e2){
return e1.id-e2.id;
})
day3
=====
Angular provides HttpClient from HttpClientModule to invoke remote service
RouterModule
Routes
day4
====
1.NestedComponent(Parent child Communcation)
Parent -> child => @Input()
Child ->Parent => @Output() =>EventEmitter
3.FormValidation
=================
1.Template Driven Forms
2.Reactive Forms/Model Driven Form
Template Driven Forms Features
===============================
Easy to use
Suitable for simple scenarios and fails for complex scenarios
Similar to AngularJS
Two way data binding(using [(NgModel)] syntax)
Minimal component code
Automatic track of the form and its data(handled by Angular)
Unit testing is another challenge
Reactive Forms Features
===========================================
More flexible, but needs a lot of practice
Handles any complex scenarios
No data binding is done (immutable data model preferred by most developers)
More component code and less HTML markup
Reactive transformations can be made possible such as
Handling a event based on a debounce time
Handling events when the components are distinct until changed
Adding elements dynamically
Easier unit testing
FormGroup,FormBuilder,Validators => ReactiveFormsModule
4.Pagination
=================
5.RouteParams and QueryParams
=============================
Use ActivatedRoute class to retrieve route params and query params
constructor(private us:UsersService,private route:ActivatedRoute) {
console.log("####### UsersComponent created ########");
}
Route Param
===========
https://jsonplaceholder.typicode.com/users/1
{path:'users/:userId',component:UsersComponent},
syntax: this.userId=this.route.snapshot.params.userId;
QueryParams
============
https://jsonplaceholder.typicode.com/posts?userId=1
syntax : this.userId=this.route.snapshot.queryParams.userId;
Angular Gurads
===============
The Angular router’s navigation guards allow to grant or remove access to certain parts of the navigation.
Note that client-side route guards like this are not meant to be a security feature. They won't prevent a clever user from figuring out a way to get to the protected routes. Such security should be implemented on the server. They are instead meant as a way to improve the UX for your apps.
Types of Guards
================
1.CanActivate:
Controls if a route can be activated.
2.CanActivateChild:
Controls if children of a route can be activated.
3.CanLoad:
Controls if a route can even be loaded. This becomes useful for feature modules
that are lazy loaded. They won’t even load if the guard returns false.
4.CanDeactivate:
Controls if the user can leave a route. Note that this guard doesn’t prevent the
user from closing the browser tab or navigating to a different address.
It only prevents actions from within the application itself.
@ViewChild
===========
1.To Inject The Component
@ViewChild(NumberComponent,{static:false})
private n:NumberComponent;
2.To Inject The Directive
@ViewChild(FgColorDirective,{static:false})
private f:FgColorDirective;
3.To Inject The ElementRef
@ViewChild("name",{static:false})
private name:ElementRef;
@ViewChild("city",{static:false})
private city:ElementRef;
Angular CRUD Application
==========================
In Express app
npm i cors
In app.js
var cors=require("cors");
app.use(cors());
Angular Express Integration
==============================
Refer below video
=====================
https://www.youtube.com/watch?v=sTbQphoYbK0
Angular Material
==================
npm i @angular/material @angular/cdk hammerjs
ng g @angular/material:material-nav --name NavBar
ng g @angular/material:material-table --name MyTab
ng g @angular/material:material-dashboard --name MyDashboard
Angular Material provides us with four themes.
================================================
deeppurple-amber.css
indigo-pink.css
pink-bluegrey.css
purple-green.css
Choose one theme and include into style.scss
=================================================
@import '@angular/material/prebuilt-themes/deeppurple-amber.css';
Angular JWT Authentication
=============================
https://github.com/cornflourblue/angular-7-jwt-authentication-example