1

This is the deleteUser function of user service which call the backend server

 deleteUser(id:number)
 {
     return this.http.delete(`${this.Url}/${id}`)
 }

and this is the userComponent which display the list of users on a grid. I want to delete user by clicking on 'supprimer'

@Component({
    template: <ul *ngFor=" let x of users">
            <li>{{x.id}}</li>
            <li>{{x.firstName}}</li>
            <li>{{x.lastname}}</li>
            <li>{{x.username}}</li>
            <li>{{x.password}}</li>
            <li><a (click)="deleteItem(x.id)">supprimer</a></li>
        </ul>
})

export class UserListComponent implements OnInit {
    private users: User[];

    constructor(private userService: UserService ,private router:Router) { }
    ngOnInit() {
        his.userService.getAllUsers().subscribe(data => {this.users = data })
    }

    deleteItem(id:number) {
        this.userService.deleteUser(id).subscribe(   );
    }

}

2 Answers 2

2

If you want to remove an item from "users" object, declare an index of the current object.

<ul *ngFor=" let x of users; let i = index;">
   <li><a (click)="deleteItem(x.id, i)">supprimer</a></li>
</ul>

Then you can splice from your collection using this index

  deleteItem(id:number, index:number) {
    this.userService.deleteUser(id).subscribe(response =>{
         this.users.splice(index, 1);
    });
 }
Sign up to request clarification or add additional context in comments.

Comments

0
user Service:  
DeleteUser(_id:any){
    return this.httpClient.delete<any>(`${environment.APIURL}/users/${_id}`,this.options)
    .pipe(map((res:any)=>{
      return res
    }))
  }

user Component.ts:
 DeleteUser(user: any) {
    this._userservice.DeleteUser(user._id).subscribe(res => {}`enter code here`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.