Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically routing url path in angularjs

I was wondering, is there a way to dynamically route the user to a specific page based on a certain parameter? For example, I may have a url that look something like this:

http://localhost:8080/admin/{{id}}/home-page.html

Here are examples of how I would want it to look like:

http://localhost:8080/admin/13/home-page.html

http://localhost:8080/admin/9/home-page.html

And based on each URL, I would navigate the user to the home-page.html file, but the data displayed on home-page.html would differ based on which id I pass in. I tried to look at https://stackoverflow.com/a/30874018/1871869 as a guide but I can’t seem to figure out where to put the $routeParams when code because unless I create another controller/page prior to home-page.html, only then can i perform the redirection. Is there any way to accomplish this? Thanks!

like image 390
user1871869 Avatar asked Apr 20 '26 07:04

user1871869


1 Answers

If you're using $routeProvider you can use :param

So for your URL about you could do

.when('/admin/:ID/home-page.html')

in your $routeProvider. Then ctrlAs.ID will be the value of your route.

If you add a question mark at the end, then that route parameter becomes optional, i.e.:

.when('/admin/:ID?')

will work with localhost:8080/admin or localhost:8080/admin/15

like image 180
Zach Schneider Avatar answered Apr 21 '26 19:04

Zach Schneider