-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroute-urls.js
More file actions
55 lines (47 loc) · 1.89 KB
/
route-urls.js
File metadata and controls
55 lines (47 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
angular.module("routeUrls", ["ngRoute"])
.provider("urls", function($locationProvider) {
return {
$get: function($route) {
// Cache the routing paths for any named routes.
var pathsByName = {};
angular.forEach($route.routes, function (route, path) {
if (route.name) {
pathsByName[route.name] = path;
}
});
// Build a path for the named route from the route's URL and the given
// params.
var path = function (name, params) {
// Accept an object or array of params.
var isObject = angular.isObject(params);
if (!isObject) {
params = Array.prototype.slice.call(arguments, 1);
}
// Iterate the path segments replacing named groups.
var path = (pathsByName[name] || "/").split("/");
for (var i=0, idx=0; i<path.length; i++) {
if (path[i] && path[i][0] === ":") {
value = isObject ? params[path[i].substring(1)] : params[idx++];
if (value) {
path[i] = value;
}
}
}
return path.join("/");
};
// Query $locationProvider for its configuration.
var html5Mode = $locationProvider.html5Mode();
var hashPrefix = $locationProvider.hashPrefix();
return {
path: path,
href: function (name, params) {
var url = path.apply(this, arguments);
if (html5Mode === true || html5Mode.enabled === true) {
return url;
}
return "#" + hashPrefix + url;
}
};
}
};
});