Here is the structure of my object array:
0: {Name: "Albert", Id: 32}
1: {Name: "George", Id: 34}
2: {Name: "Jane", Id: 35}
Although the request is successful, the array remains unchanged. However, upon refreshing the app, the item (student) is deleted. When I attempt to delete by clicking the button, I receive the following error: INDEX:-1
What could be the issue here?
student.component.html
<thead>
<tr style="font-weight: 500;font-size:15px;">
<th data-field="id">Id</th>
<th data-field="name">Display Name</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let student of students;">
<td>{{ student.Id }}</td>
<td>{{ student.Name }}</td>
<td><span class="ion-android-delete" (click)="onDelete(student.Id)"> </span></td>
</tr>
</tbody>
student.component.ts
onDelete(studentId: number) {
if (window.confirm('Are you sure you want to delete?')) {
var index = this.students.indexOf(studentId, 1);
while (this.students.indexOf(studentId) !== -1) {
this.students.splice(this.students.indexOf(studentId), 1);
}
this.studentService.deleteStudent(studentId)
.subscribe(
(response) => console.log(response));
(error) => console.log(error));
}
}
student.service.ts
deleteStudent(studentId: number) {
return this.http.delete......
(response: Response) => {
const data = response.json();
return data;
}
)
.catch(
(error: Response) => {
console.log(error);
return Observable.throw(error);
}
);
}
Modified code snippet:
onDeleteAgent(agentId: number) {
if (window.confirm('Are you sure you want to delete?')) {
while (this.agents.indexOf(this.agents) !== -1) {
console.log(this.agents.indexOf(this.agents));
this.agents.splice(this.agents.indexOf(this.agents), 1);
}
this.agentService.deleteAgent(agentId)
.subscribe(
(response) => console.log(response),
(error) => console.log(error));
}
}