Created
March 3, 2025 13:00
-
-
Save Armenvardanyan95/91022b96275ea50be99c1833a68d0a21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
template: ` | |
@if (todos.hasValue()) { | |
<select [(ngModel)]="todoId"> | |
@for (todo of todos.value(); track todo.id) { | |
<option [value]="todo.id">{{ todo.title }}</option> | |
} | |
</select> | |
} | |
@if (todo.hasValue()) { | |
<div> | |
<h1>{{ todo.value().title }}</h1> | |
<p>{{ todo.value().completed }}</p> | |
</div> | |
} | |
@if (todos.isLoading() || todo.isLoading()) { | |
<div>Loading...</div> | |
} | |
`, | |
}) | |
export class HttpResourceComponent { | |
todoId = signal<number | null>(1); | |
todos = httpResource<Todo[]>( | |
() => `https://jsonplaceholder.typicode.com/todos`, | |
); | |
todo = httpResource<Todo>( | |
// as soon as the user selects a new todo | |
// the `todoId` signal will change its value | |
// and the `todo` resource will be reloaded | |
() => `https://jsonplaceholder.typicode.com/todos/${this.todoId()}`, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment