Skip to content

Instantly share code, notes, and snippets.

@Armenvardanyan95
Created March 3, 2025 13:10
Show Gist options
  • Save Armenvardanyan95/b1db5e1f0212a4b9774fe0814e722e8d to your computer and use it in GitHub Desktop.
Save Armenvardanyan95/b1db5e1f0212a4b9774fe0814e722e8d to your computer and use it in GitHub Desktop.
@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>
}
@if (todos.error()) {
<!-- retry on error -->
<button (click)="todos.reload()">Retry</button>
}
`,
imports: [FormsModule],
})
export class HttpResourceComponent {
todoId = signal<number | null>(1);
todos = httpResource<Todo[]>(
() => `https://jsonplaceholder.typicode.com/todos`,
);
todo = httpResource<Todo>(
() => `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