-
Notifications
You must be signed in to change notification settings - Fork 2
/
Angular.ts
304 lines (228 loc) · 6.64 KB
/
Angular.ts
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
// ANGULAR - Javascript Framework - by Beumsk
/**/
// npm install @angular/cli -g
// npm i @angular/cli -g
// ng new <projectName>
// npm install
// npm install tslint
// npm install --save json-server
// ng generate component <componentName>
// ng g c <componentName>
// ng g c <folder>/<componentName>
// ng g service <serviceName>
// ng serve
// npm start
// npm run <custom-script from package.json>
// ng build
// ng build --prod
// prod files are now in dist/<projectName>
// more cli commands -> https://malcoded.com/posts/angular-fundamentals-cli/
// Angular cheat sheet -> https://angular.io/guide/cheatsheet
// COMPONENTS
import { Component, ViewEncapsulation } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'name-component', // <name-component> in HTML
encapsulation: ViewEncapsulation.None, // disables encapsulation (shadow dom) -> css is applied everywhere
templateUrl: './name.component.html',
styleUrls: ['./name.component.css']
})
export class NameComponent {
public pretrad: string;
public show: boolean;
constructor (
private router: Router
) {
this.pretrad = 'MODULES.NAME.';
this.show = true;
}
public ngOnInit(): void {}
public toggleShow(): void {
this.show = !this.show;
}
}
// MODULES
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent,
],
imports: [
CommonModule,
BrowserModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
// SERVICES
// in a model ts file
export class ModelName {
id: number;
date: Date;
weight: number;
}
// in a .service.ts file
import { Injectable } from "@angular/core";
import { ModelName } from "./modelName";
@Injectable({
providedIn: 'root???'
})
export class NameService {
public arr: ModelName[] = [
{id:1, date:new Date(), weight:75},
{id:2, date:new Date(), weight:80},
{id:3, date:new Date(), weight:85},
]
constructor(
public namesSvc: NameService
) {}
}
// INPUT (parent to child)
// .html (parent)
@component({ template: `
<child-component [show]="show"></child-component>
`})
// .component.ts (child)
import { Component, OnInit, Input } from '@angular/core';
@Component({})
export class ChildrenComponent implements OnInit {
@Input() show: boolean;
}
// OUTPUT (child to parent)
// .component.ts (child)
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
@Component({})
export class ChildrenComponent implements OnInit {
@Output() create = new EventEmitter();
constructor() {}
public createSmth(): void {
this.create.emit("Any data you want in your parent");
}
}
// .html (parent)
`<child-component (create)="createSmthElse($event)"></child-component>`
// .component.ts (parent)
public createSmthElse(line: string): void {
console.log(line);
}
// VIEWCHILD (bind HTML elements with Angular elements)
// .component.ts
import { AfterViewInit, Component, ViewChild } from '@angular/core';
@Component({})
export class ChildrenComponent implements AfterViewInit {
@ViewChild('htmlElement') htmlElement: any;
ngAfterViewInit() {
console.log(this.htmlElement);
}
}
// .html
`<any-element #htmlElement></any-element>`
// HTML
// inline js
`<p>{{ namesSvc.arr[0].id }}</p>`
`<p>{{ namesSvc.arr[0].date | date:'dd/MM/yyyy' }}</p>`
`<p>{{ "lowercase" | uppercase }}</p>`
// events
`<div (click)="toggleShow()"></div>`
`<p [hidden]="!show"></p>`
`<p *ngIf="show"></p>`
// loops
`<p *ngFor="let name of namesSvc.arr; index as i">{{ name.weight }}</p>`
// child component
`<div>
<child-component></child-component>
</div>`
// attributes
[ngClass]="{'my-class': true}"
[ngClass]="{'class-1': true, 'class-2': true}"
[ngClass]="true ? 'class-1' : 'class-2'"
[ngClass]="[true ? 'class-1' : 'class-2', true ? 'class-3 : class-4']"
[src]=""
[alt]=""
[anything]=""
// CSS
// access the host element of a component
:host {
display: block; // needed to see this element
}
// access the host element (with a certain class) of a component
:host(.example) {
display: block; // needed to see this element
}
// access child element from parent element
.parent ::ng-deep .child {}
// TRANSLATE
// in en.json
{
"anything": "Anything",
"withVariable": "With a variable of {{var}}",
"item": "Item",
"item_plural": "Items"
}
// in .html
`<p>{{ 'anything' | translate }}</p>` // Anything
`<p>{{ 'withVariable' | translate:{var:'anything'} }}</p>` // With a variable of anything
`<p>{{ 'item' | translate:{count:1} }}</p>` // Item
`<p>{{ 'item' | translate:{count:2} }}</p>` // Items
// ROUTER
// router module
import { RouterModule } from '@angular/router'
imports: [RouterModule.forRoot([
{path: '', redirectTo: '/home', pathMatch: 'full'},
{path: 'home', component: HomeComponent},
{path: 'entries/:id', component: EntryDetailsComponent}
])]
// calling router
`<router-outlet></router-outlet/>`
`<a [routerLink]="['/home']">Link</a>`
this.router.navigate(['/home']);
// FORM
// import FormsModule in app.module.ts
import { FormsModule } from '@angular/forms';
@NgModule({imports:[FormsModule]})
// model property to hold the data in ts file
model;
public ngOnInit(): void {
this.resetForm();
}
public createSmth(): void {
this.create.emit(this.model);
}
public resetForm(): void {
this.model = {};
}
// create the form and bind it
`<form #entryForm="ngForm">
<label for="name">Name</label>
<input type="text" name="name" id="name" [(ngModel)]="model.name" required>
<button [disabled]="entryForm.form.invalid" (click)="createSmth(); resetForm();">Save</button>
</form>`
// HTTP
// RXJS
map() // to format data
tap() // to look at values
subscribe() // to be the end client
// import HTTP
import { HttpClientModule } from '@angular/common/http';
@NgModule({imports:[HTTPClientModule]})
// GET
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) {}
public getEntries() {
return this.http.get('/api/entries').pipe(
map(entries => {
return entries.map(e => {
e.date = new Date(e.date);
return e;
})
})
)
}
// POST
addEntry(entry: Entry) {
return this.http.post('/api/entries', entry)
}