$urlRouterProvider
(service in module ui.router.router
)
$urlRouterProvider
has the responsibility of watching $location
.
When $location
changes it runs through a list of rules one by one until a
match is found. $urlRouterProvider
is used behind the scenes anytime you specify
a url in a state configuration. All urls are compiled into a UrlMatcher object.
There are several methods on $urlRouterProvider
that make it useful to use directly
in your module config.
Defines a path that is used when an invalied route is requested.
rule – {string|object} –
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
services.
{object}
– $urlRouterProvider - $urlRouterProvider instance
var app = angular.module('app', ['ui.router.router']); 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) { ... }); });
Defines rules that are used by `$urlRouterProvider to find matches for specific URLs.
rule – {object} –
Handler function that takes $injector
and $location
services as arguments. You can use them to return a valid path as a string.
{object}
– $urlRouterProvider - $urlRouterProvider instance
var app = angular.module('app', ['ui.router.router']); app.config(function ($urlRouterProvider) { // Here's an example of how you might allow case insensitive urls $urlRouterProvider.rule(function ($injector, $location) { var path = $location.path(), normalized = path.toLowerCase(); if (path !== normalized) { return normalized; } }); });
Registers a handler for a given url matching. if handle is a string, it is treated as a redirect, and is interpolated according to the syyntax of match (i.e. like String.replace() for RegExp, or like a UrlMatcher pattern otherwise).
If the handler is a function, it is injectable. It gets invoked if $location
matches. You have the option of inject the match object as $match
.
The handler can return
$urlRouter
will continue trying to find another one that matches.$location.url()
$urlRouter
that the url was handled.what – {string|object} –
The incoming path that you want to redirect.
handler – {string|object} –
The path you want to redirect your user to.
var app = angular.module('app', ['ui.router.router']); app.config(function ($urlRouterProvider) { $urlRouterProvider.when($state.url, function ($match, $stateParams) { if ($state.$current.navigable !== state || !equalForKeys($match, $stateParams) { $state.transitionTo(state, $match, false); } }); });