Usually in a Single Page App(spa), I do have a single page have where I have a sidenav menu. In that menu, there are multiple anchor tags.
Those anchor tag's url will be handled by angular/react/sammy js router, and the main div would be refreshed based on the returned html from the controller.
Pretty simple, right?
But imagine a scenario, where user directly access the anchor tag url via browser address bar. then only the returned html segment would be loaded to the whole page.
Is there any way to handle this kind of situation; I mean so that whenever user directly access the url, he/she would be properly addressed?
EDIT:
May be I'm not so clear about the problem statement. Let me elaborate a bit:
- Suppose my page url is: abc.com/dashboard
- This page got a navigation menu and a div section whose class name is "main-container"
- User click a link in the nav menu and the router moved the url to for say abc.com/view/listofXYZ. So, our "main-container" div would be loaded with the response of the url abc.com/view/listofXYZ
- Now another user, directly go to the abc.com/view/listofXYZ url and hit eneter. Then, the page would contain only the response html of the url i.e. all nav menu and div are gone.
My question is, can we implement some design approach, so that these two works well?
Better go for angular
ui-routerinstead ofsammy js router, Of course I don't know about it. But inangular-ui-router, we define states and urls by using$stateProviderofui-router. Even user entered the url directly in the browser, he wil be addressed correctly only if the url defined in the state matched.https://github.com/angular-ui/ui-router/tree/legacy see here for more info.
Here is an example,
Use
$urlRouterProvider.otherwise('/')if no path matched Likewise, user can traverse fromhometolistfor example,www.my-app.comis your host name. If he enteredwww.my-app.com/home/listas url/home/listmatched, he will be taken tohome/listpage and if he entered any wrong url, then he will be taken tohomepage because ofotherwisemethod.If there is something like this in the react/sammy router, do that for better solution