I've searched a lot online for an answer to this but haven't found anything yet.
Right now I have this code in my file that I thought would handle all routes/states besides the ones I've specified:
$urlRouterProvider.otherwise('/');
However, this functionality only works for paths like localhost:1337/#/stuff
, but if I type in localhost:1337/stuff
, I get an ugly internal server error.
Ideally, I'd like localhost:1337/stuff
or any other URL without a hashbang to redirect to localhost:1337/#/
(my app's homepage).
Any thoughts on how I can accomplish this?
I started with Angular 2+, but my current company has some projects that require AngularJS 1x. So, here we are! I this post, I will show you how to handle AngularJS 1x routing by using Angular-Route (ngRoute). There will be another post that I will use UI-Router for handling AngularJS routing.
AngularJS Angular-UI-Router is an AngularJS module used to create routes for AngularJS applications. Routes are an important part of Single-Page-Applications (SPAs) as well as regular applications and Angular-UI-Router provides easy creation and usage of routes in AngularJS.
To handle such scenario angular router allows us to include route parameters, where we can send any dynamic value for a URL segment We can define parameter by adding forward slash followed colon and a placeholder (id) as shown below Now above path matches the URLs /product/1 , /product/2, etc.
The following command uses the Angular CLI to generate a basic Angular app with an app routing module, called AppRoutingModule, which is an NgModule where you can configure your routes. The app name in the following example is routing-app. When generating a new app, the CLI prompts you to select CSS or a CSS preprocessor.
You need to set up two things:
$locationProvider.html5Mode(true);
From AngularJS docs on routing:
Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With