I want the data in input fields to be removed when I click on the current page link

Viewed 50

I work with Angular 8. I added onSameUrlNavigation: 'reload' to my router in order to make the second click on the same link reload the page. It works fine but it doesn't empty the input fields on the reloaded page. Why is that so and how can I fix it?

1 Answers

The onSameUrlNavigation: 'reload' reruns the router but it keeps active the already rendered component and so on its current state. A workaround is to listen to router events into the component, acting when the navigation ends :

ngOnInit(): void {
    this.router.events.pipe(
      // when the navigation ends
      filter((event: RouterEvent) => event instanceof NavigationEnd)
    ).subscribe(() => {
      // empty the input fiels programatically
    });
  }
Related