Skip to content

Instantly share code, notes, and snippets.

@Armenvardanyan95
Created March 10, 2025 10:34
Show Gist options
  • Save Armenvardanyan95/3e49c9a2dcde2f6f989781e088bae17c to your computer and use it in GitHub Desktop.
Save Armenvardanyan95/3e49c9a2dcde2f6f989781e088bae17c to your computer and use it in GitHub Desktop.
@Component({
template: `
@if (todo.hasValue()) {
<form (ngSubmit)="save()">
<input [(ngModel)]="todo.value().title" />
<button type="submit">Save</button>
</form>
}
`,
imports: [FormsModule],
})
export class HttpResourceComponent {
readonly #http = inject(HttpClient);
todoId = signal<number | null>(1);
todo = httpResource<Todo>(
() => `https://jsonplaceholder.typicode.com/todos/${this.todoId()}`,
);
save() {
if (this.todo.hasValue()) {
this.#http
.put(
`https://jsonplaceholder.typicode.com/todos/${this.todoId()}`,
this.todo.value(),
)
.subscribe(
// handle success
);
}
}
delete() {
if (this.todo.hasValue()) {
this.#http
.delete(
`https://jsonplaceholder.typicode.com/todos/${this.todoId()}`,
)
.subscribe(
// handle success
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment