I have an AngularJS application that is using ui-router. All is working okay but I would like to have a user registration confirmation screen accessed like this:
<
I would say that what we need here is - to let the index.html
be laoded - as a SPA (Single page application). Then we will profit from the features built in in the UI-Router:
.when() for redirection
Parameters:
what String | RegExp | UrlMatcher The incoming path that you want to redirect.
handler String | Function The path you want to redirect your user to.
handler
as StringIf handler is a string, it is treated as a redirect, and is interpolated according to the syntax of match (i.e. like String.replace() for RegExp, or like a UrlMatcher pattern otherwise).
app.config(function($urlRouterProvider){
// when there is an empty route, redirect to /index
$urlRouterProvider.when('', '/index');
// You can also use regex for the match parameter
$urlRouterProvider.when(/aspx/i, '/index');
})
.otherwise() for invalid routes
Parameters:
path String | Function The url path you want to redirect to or a function rule that returns the url path. The function version is passed two params: $injector and $location.
app.config(function($urlRouterProvider){
// if the path doesn't match any of the urls you configured
// otherwise will take care of routing the user to the specified url
$urlRouterProvider.otherwise('/index');
// Example of using function rule as param
$urlRouterProvider.otherwise(function($injector, $location){
... some advanced code...
});
})
How to use the .when()
properly (e.g. order of declaration) please check also here (with more details and examples)
It's quite simple actually, but requires work at server-side: the server must be configured to serve the index.html page for the path /Auth/confirmation
(and for all the other bookmarkable URLs of your app).
Once that is true, a user going to /Auth/confirmation
will thus download the index.html file, which will start the angular application. The ui-router module will analyze the location, load the corresponding state, and display the corresponding view.