mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-11 21:33:02 -05:00
feat(doc): move crud
This commit is contained in:
51
apps/angular/crud/src/app/app.component.ts
Normal file
51
apps/angular/crud/src/app/app.component.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { randText } from '@ngneat/falso';
|
||||
|
||||
@Component({
|
||||
standalone: true,
|
||||
imports: [CommonModule],
|
||||
selector: 'app-root',
|
||||
template: `
|
||||
<div *ngFor="let todo of todos">
|
||||
{{ todo.title }}
|
||||
<button (click)="update(todo)">Update</button>
|
||||
</div>
|
||||
`,
|
||||
styles: [],
|
||||
})
|
||||
export class AppComponent implements OnInit {
|
||||
todos!: any[];
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.http
|
||||
.get<any[]>('https://jsonplaceholder.typicode.com/todos')
|
||||
.subscribe((todos) => {
|
||||
this.todos = todos;
|
||||
});
|
||||
}
|
||||
|
||||
update(todo: any) {
|
||||
this.http
|
||||
.put<any>(
|
||||
`https://jsonplaceholder.typicode.com/todos/${todo.id}`,
|
||||
JSON.stringify({
|
||||
todo: todo.id,
|
||||
title: randText(),
|
||||
body: todo.body,
|
||||
userId: todo.userId,
|
||||
}),
|
||||
{
|
||||
headers: {
|
||||
'Content-type': 'application/json; charset=UTF-8',
|
||||
},
|
||||
}
|
||||
)
|
||||
.subscribe((todoUpdated: any) => {
|
||||
this.todos[todoUpdated.id - 1] = todoUpdated;
|
||||
});
|
||||
}
|
||||
}
|
||||
6
apps/angular/crud/src/app/app.config.ts
Normal file
6
apps/angular/crud/src/app/app.config.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { ApplicationConfig } from '@angular/core';
|
||||
import { enableProdMode, importProvidersFrom } from '@angular/core';
|
||||
import { HttpClientModule } from '@angular/common/http';
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [importProvidersFrom(HttpClientModule)],
|
||||
};
|
||||
Reference in New Issue
Block a user