1

I have an Component that display a list of projects, with Edit and Delete buttons

If i click delete:

onDelete(projectName: string): void {
    this.projectService.deleteProject(projectName);

    this.router.navigate(['/projects']);
}

My router component:

export const projectListRouters: Routes = [
{
    path: '',
    children: [
        {path: '', component: URLComponent},
        {path: 'login', component: Login},
        {path: 'projects', canActivate: [AuthGuard], component: ProjectListComponent},

        {path: 'project/new/-', component: ProjectDetails, canActivate: [AuthGuard], data: {oparationType: 'new'}},

        // otherwise redirect to home
        {path: '**', redirectTo: ''}
    ],
},

];

My problem is that the this.router.navigate(['/projects']) does not refresh the list after deleting the project,

user513951
  • 12,445
  • 7
  • 65
  • 82
Yosefarr
  • 709
  • 5
  • 12
  • 26

3 Answers3

2

I presume you are redirecting to the same page.If that's the case, then please update your model say projectList that stores the list of project names.

If deleteProject is async call, then set this.router.navigate(['/projects']); in promise, otherwise just removing the element from projectList will do.

Hope it helps!!

Jayakrishnan
  • 4,232
  • 2
  • 23
  • 35
0

There is now way to refresh component at all. You can create dummy component, redirect to it and redirect back to /projects.

After you make deletion of an item, you should just make a call to backend to retrieve list of projects as you did when you load component.

Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
0

Rather you can load your project list in subscribe of service.

delete() {

        this._service.delete(this.id).
            subscribe(x=> {
                // call your list here.
                this.projectList();
               }, error => {
                alert(error);
            });
    }
Yashasvi
  • 197
  • 1
  • 3
  • 6