Skip to content

Commit a8f9310

Browse files
committed
Update playground
1 parent 17d6f3a commit a8f9310

File tree

2 files changed

+59
-46
lines changed

2 files changed

+59
-46
lines changed

playground/app/app.component.html

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
1-
<h1>Angular State</h1>
2-
<h2>Playground</h2>
3-
4-
<h3>Todos</h3>
5-
6-
<input
7-
type="text"
8-
placeholder="New Todo"
9-
#todoInput
10-
(keydown.enter)="addTodo(todoInput.value); todoInput.value = ''"
11-
/>
12-
13-
<ng-container *ngIf="uncompletedTodos$ | async; let todos">
14-
<h4 *ngIf="todos.length > 0">Open</h4>
15-
16-
<ul>
17-
<li *ngFor="let todo of uncompletedTodos$ | async">
18-
<div>
19-
<span>{{ todo.description }}</span>
20-
<button (click)="completeTodo(todo)">Complete</button>
21-
</div>
22-
</li>
23-
</ul>
24-
</ng-container>
25-
26-
<ng-container *ngIf="completedTodos$ | async; let todos">
27-
<h4 *ngIf="todos.length > 0">Completed</h4>
28-
29-
<ul>
30-
<li *ngFor="let todo of todos">
31-
<div>
32-
<span>{{ todo.description }}</span>
33-
<button (click)="uncompleteTodo(todo)">Uncomplete</button>
34-
</div>
35-
</li>
36-
</ul>
37-
</ng-container>
1+
<h1>Angular State</h1>
2+
<h2>Playground</h2>
3+
4+
<h3>Todos</h3>
5+
6+
<input
7+
type="text"
8+
placeholder="New Todo"
9+
#todoInput
10+
(keydown.enter)="addTodo(todoInput.value); todoInput.value = ''"
11+
/>
12+
13+
@if (uncompletedTodos$().length > 0) {
14+
<h4>Open</h4>
15+
16+
<ul>
17+
@for (todo of uncompletedTodos$(); track todo.id) {
18+
<li>
19+
<div>
20+
<span>{{ todo.description }}</span>
21+
<button (click)="completeTodo(todo)">Complete</button>
22+
</div>
23+
</li>
24+
} @empty {
25+
No uncompleted todos
26+
}
27+
</ul>
28+
}
29+
30+
@if (completedTodos$().length > 0) {
31+
<h4>Completed</h4>
32+
33+
<ul>
34+
@for (todo of completedTodos$(); track todo.id) {
35+
<li>
36+
<div>
37+
<span>{{ todo.description }}</span>
38+
<button (click)="completeTodo(todo)">Uncomplete</button>
39+
</div>
40+
</li>
41+
} @empty {
42+
No uncompleted todos
43+
}
44+
</ul>
45+
}

playground/app/app.component.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { ChangeDetectionStrategy, Component } from '@angular/core';
2-
import { map, Observable } from 'rxjs';
1+
import {
2+
ChangeDetectionStrategy,
3+
Component,
4+
Signal,
5+
computed,
6+
} from '@angular/core';
37
import { Todo, TodoStoreService } from './services/todo-store.service';
48

59
@Component({
@@ -9,16 +13,17 @@ import { Todo, TodoStoreService } from './services/todo-store.service';
913
changeDetection: ChangeDetectionStrategy.OnPush,
1014
})
1115
export class AppComponent {
12-
readonly completedTodos$: Observable<Todo[]>;
13-
readonly uncompletedTodos$: Observable<Todo[]>;
16+
readonly completedTodos$: Signal<Todo[]>;
17+
readonly uncompletedTodos$: Signal<Todo[]>;
1418

1519
constructor(private todoStore: TodoStoreService) {
16-
const allTodos$ = this.todoStore.select(state => state.todos);
17-
this.completedTodos$ = allTodos$.pipe(
18-
map(todos => todos.filter(t => !!t.completed))
20+
const allTodos$ = this.todoStore.selectAsSignal(state => state.todos);
21+
22+
this.completedTodos$ = computed(() =>
23+
allTodos$()?.filter(t => t.completed)
1924
);
20-
this.uncompletedTodos$ = allTodos$.pipe(
21-
map(todos => todos.filter(t => !t.completed))
25+
this.uncompletedTodos$ = computed(() =>
26+
allTodos$()?.filter(t => !t.completed)
2227
);
2328
}
2429

0 commit comments

Comments
 (0)