Created
March 10, 2025 10:34
-
-
Save Armenvardanyan95/3e49c9a2dcde2f6f989781e088bae17c 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 (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