Let me share an innovative approach we adopted for our Angular app, which required backend-defined routes. When the user enters the app with an unknown URL at runtime, here's what we do:
- We call a service to define our app URLs.
- We validate the retrieved URLs.
- We use
$route.reload()
to essentially "restart" the application (although it doesn't actually restart the app, just runs the route parser again).
We then establish routes based on the app's features, such as http://foo.bar/product/:slug
. So, if a user lands on http://foo.bar/product/foobar
, our app has no prior knowledge of whether foobar
exists. This approach allows us to maintain a simple UI and delegate route definitions to the backend; hence...
- In your routes config, set up base routes like
/product/:slug
, /page/:slug
, etc.
- Create a factory that tracks a boolean variable indicating whether the app has loaded localization data. If false, your
product
, page
, etc., controllers will halt at their init
.
- In your base controller, load localization data, validate it, and so forth. Once satisfied, trigger
$route.reload()
and update the factory flag to true.
- Your
product
, page
, etc., controllers will be invoked again, this time with the factory flag set to true, enabling them to access the loaded localization data.
- Success!
This process might seem complex, but it can indeed be an elegant solution that functions smoothly. Just ensure you have a friendly loader displayed during the process.