Search code examples
angularangular-routing

Angular 2 router attaches question mark in URL


I am facing a strange issue in my Angular 4 project. I am trying to navigate to a path through code (on button click). I am using router.navigate() method as follows to accomplish the job --

this._router.navigate(["/employeeDetails", selectedEmployee.EmployeeId]);

Where selectedEmployee.EmployeeId is a number. The navigation happens but I am getting a very weird thing in the URL. Initially the URL shows as follows -- http://localhost:4200/?employeeDetails/170 and then the ? sign vanishes from the URL and the required page is loaded.

Can anyone please tell me why the ? sign is coming in the URL. Ideally it should load the respective component for the route "/employeeDetails" without refreshing the page. I also tried the code without / as follows but no help.

this._router.navigate(["/employeeDetails", selectedEmployee.EmployeeId]);

Any help would be appreciated.


Solution

  • Reason could be a missing action="/formsubmit". When Chrome tries to send a GET request without it this it appends a ?to the current URL.

    Try the below for your submit action:

    onSubmitAction(event){
        event.preventDefault()
    }
    
    • It is not an issue with the Angular router

    Hope it helps :)