forked from tomalaforge/angular-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.component.ts
More file actions
46 lines (42 loc) · 1.38 KB
/
app.component.ts
File metadata and controls
46 lines (42 loc) · 1.38 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
import { AsyncPipe } from '@angular/common';
import { Component } from '@angular/core';
import { randFirstName } from '@ngneat/falso';
import { BehaviorSubject } from 'rxjs';
import { RandomComponent } from './components/random.component';
import { PersonListComponent } from './container/person-list.component';
@Component({
imports: [AsyncPipe, PersonListComponent, RandomComponent],
selector: 'app-root',
template: `
<app-random />
<div class="flex">
<app-person-list
[names]="girlList$ | async"
title="Female"
(newName)="addName($event)" />
<app-person-list
[names]="boyList$ | async"
title="Male"
(newName)="addName($event)" />
</div>
`,
})
export class AppComponent {
private girlListSubject = new BehaviorSubject<string[]>(
randFirstName({ gender: 'female', length: 10 }),
);
private boyListSubject = new BehaviorSubject<string[]>(
randFirstName({ gender: 'male', length: 10 }),
);
girlList$ = this.girlListSubject.asObservable();
boyList$ = this.boyListSubject.asObservable();
addName({ title, name }: { title: string; name: string }) {
if (title === 'Female') {
const current = this.girlListSubject.value;
this.girlListSubject.next([name, ...current]);
} else {
const current = this.boyListSubject.value;
this.boyListSubject.next([name, ...current]);
}
}
}