Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Change router so route can be a callback function #2834

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/ng/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ function $RouteProvider(){
* Adds a new route definition to the `$route` service.
*/
this.when = function(path, route) {
routes[path] = extend({reloadOnSearch: true, caseInsensitiveMatch: false}, route);
routes[path] = isFunction(route)
? route
: extend({reloadOnSearch: true, caseInsensitiveMatch: false}, route);

// create redirection for trailing slashes
if (path) {
Expand Down Expand Up @@ -477,8 +479,16 @@ function $RouteProvider(){
var params, match;
forEach(routes, function(route, path) {
if (!match && (params = switchRouteMatcher($location.path(), path, route))) {
var search = $location.search();

if (isFunction(route)) {
// route is a function, call it to get the actual route to use
// route function can update params or search
route = route(params, search);
}

match = inherit(route, {
params: extend({}, $location.search(), params),
params: extend({}, search, params),
pathParams: params});
match.$$route = route;
}
Expand Down