Update front fields in the database

Viewed 81

I have a project in Angular and Spring in which I am implementing CRUD operations but I am having trouble updating the fields in the database.

My problem: how can I communicate the ts function with the service to send the new data to the back and update the database?

This is my service.ts with the edit function:

public edit(cliente: Cliente): Observable<Cliente> {
      console.log('edit: ' + `${PREFIX}/${cliente.id}`, cliente);
      return this.http.post<Cliente>(`${PREFIX}/${cliente.id}`, cliente);
    }

This is the function that I currently have in the ts to save the changes but I don't know how to communicate it with the service to send it the new data:

saveEdit() {
    this.editIndex = -1;
  }
1 Answers
  • You have to import your service on your component. ej
@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.scss']
})
import {MyService} from 'path/to/my-service.ts';
export class EditComponent{
constructor(private myService: MyService){}
}
  • Then use it in saveEdit method, ej
saveEdit():void {
const client = .....; // I dont know how do you get this, maybe from a form like `this.myForm.value`
 this.myService.edit(client).subscribe({ next:(response) => {
console.log(response);
// Here the success response
}, error: (e) => {
console.log(e);
// Here on error
} });
}

PS. I dont speak english very well

Related