Guest User

Untitled

a guest
Nov 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import {UsersService} from "./services/users.service";
  3. import {User} from "./services/user";
  4.  
  5. @Component({
  6. selector: 'app-users',
  7. templateUrl: './users.component.html',
  8. styleUrls: ['./users.component.css']
  9. })
  10. export class UsersComponent implements OnInit {
  11.  
  12. private users: User[] = [];
  13.  
  14. constructor(private usersService: UsersService) { }
  15.  
  16. ngOnInit() {
  17. this.usersService.getUsers()
  18. .subscribe(data => this.users = data);
  19. }
  20.  
  21. deleteUser(user){
  22. if (confirm("Are you sure you want to delete " + user.name + "?")) {
  23. var index = this.users.indexOf(user);
  24. this.users.splice(index, 1);
  25.  
  26. this.usersService.deleteUser(user.id)
  27. .subscribe(null,
  28. err => {
  29. alert("Could not delete user.");
  30. // Revert the view back to its original state
  31. this.users.splice(index, 0, user);
  32. });
  33. }
  34. }
  35.  
  36. }
Add Comment
Please, Sign In to add comment