Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit } from '@angular/core';
- import {UsersService} from "./services/users.service";
- import {User} from "./services/user";
- @Component({
- selector: 'app-users',
- templateUrl: './users.component.html',
- styleUrls: ['./users.component.css']
- })
- export class UsersComponent implements OnInit {
- private users: User[] = [];
- constructor(private usersService: UsersService) { }
- ngOnInit() {
- this.usersService.getUsers()
- .subscribe(data => this.users = data);
- }
- deleteUser(user){
- if (confirm("Are you sure you want to delete " + user.name + "?")) {
- var index = this.users.indexOf(user);
- this.users.splice(index, 1);
- this.usersService.deleteUser(user.id)
- .subscribe(null,
- err => {
- alert("Could not delete user.");
- // Revert the view back to its original state
- this.users.splice(index, 0, user);
- });
- }
- }
- }
Add Comment
Please, Sign In to add comment