Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture URLs with arbitrary number of slashes in angular.js?

Tags:

angularjs

I want to make an angular site dealing with files where users can navigate around in a file system tree, (like on github: github.com/angular/angular.js/tree/master/path/to/my/file.js.

I would like to capture that path/to/my/file.js part of the URL with an angular route:

.when("/files/:myPath", templateUrl: "...", controller: "...")

but as expected, :myPath only matches up to the next slash.

How can I capture all remaining parts of the URL, including an arbitrary number of slashes?


I found this question that is related, but differs in that my URL is fine to be after the angular hash, e.g. .../index.html#/files/path/to/my/file.

like image 288
nh2 Avatar asked Apr 26 '13 00:04

nh2


1 Answers

As of Angular 1.1.5 you can use *path - see here.

path can contain named groups starting with a star (*name). All characters are eagerly stored in
$routeParams under the given name when the route matches.

For example, routes like /color/:color/largecode/*largecode/edit will match /color/brown/largecode/code/with/slashs/edit

As of Angular 1.3, the syntax is :path* (credit to DRAX in the comments)

like image 183
tungd Avatar answered Nov 03 '22 12:11

tungd