Search code examples
angularangular2-routingangular2-servicesangular2-router3

Manually changing the route parameters leads to inconsistent user interface


Here is a plunker to play with:

https://plnkr.co/edit/qTjftING3Hk2fwN36bQa?p=preview

Everything works well, except when you manually change the id parameter in the url bar, because then the projects dropdown does not show the new projectId as current project. This happens when the user saves an url as favorite link and copy/paste it into the url bar! A common case I would say!

To fix this I can listen to route.params changes in the TestsListComponent and add a check with a sharedService/EventEmitter wheter the changed id exists in that dropdown. The bool return value existsProjectId inside the TestsListComponent decides wether I should redirect to the /projects page because the id did not exist.

But honestly, redirecting from the TestsListComponent is too late at least from a user experience perspective, because the route projects/:id/tests is already activated.

How would you fix that misbehavior?

P.S.


Solution

  • I'm not sure if i correctly understand your question, anyway here is my plunkr.

      this.router.events.subscribe(event => {
    
            if (event instanceof RoutesRecognized)
            {
              var first=event.state.root.firstChild.firstChild;
              var id = first && event.state.root.firstChild.firstChild.params['id'];
              console.debug('recognized', id);
              this.currentProject = this.projects[+id-1];
    
              console.log(this.currentProject);
            } 
      });
    

    Just navigate to #/projects/1/tests.