我可以在数据库中添加和更新表单上的数据,但无法从同一数据库中删除保存的数据。我的问题是如何根据表单文本框值删除数据库记录。
组件.ts
public orderForm = new FormGroup({
Id: new FormControl(),
Name: new FormControl(),
Address: new FormControl(),
});
delete() {
if(this.orderForm.valid){
this.service2.deleteData(this.orderForm.value).subscribe((result) =>{
console.log(result);
this.toastr.success('Record Deleted Successful');
});
}
else
{
this.toastr.error('Please Select Record to Delete ');
}
}
共享服务.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { employeeData } from './data-model';
const httpOptions = {headers: new HttpHeaders({
"Content-Type": "application/json"
})};
@Injectable({
providedIn: 'root'})
export class SharedService {
public APIUrl= "http://localhost:5000/api/5mployees";
constructor(private http:HttpClient) {
}
getcropplanting(state: any): Observable<any> {
console.log(state);
return this.http.get<any>(this.APIUrl);
}
addRecord(state: any): Observable<any> {
return this.http.post<any>(this.APIUrl, state.data, httpOptions);
}
updateRecord(state: any): Observable<any> {
return this.http.put<any>(this.APIUrl, state.data, httpOptions);
}
deleteData(id: number): Observable<any> {
return this.http.delete(`${this.APIUrl}/${id}`)
}
}
来自
deleteData
的 SharedService
需要 1 个 number
类型的参数。目前,您提供来自对象 this.orderForm.value
的值,您将收到编译错误。
相反,您应该向呼叫者提供
this.orderForm.value.Id
。
this.service2.deleteData(this.orderForm.value.Id).subscribe((result) =>{
console.log(result);
this.toastr.success('Record Deleted Successful');
});