简体   繁体   中英

How to do nested routing using one template & controller in angularjs?

I have a main template main.html, now I have say few links in this page.

  `<a href="albums/bollywood">bollywood </a>`
  <a href="albums/tollywood">tollywood </a>
  • Now in turn bollywood page, has again links

    <a href="album/bollywood/hindi/"> Hindi </a>

Now in turn this page again have links as <a href="album/bollywood/hindi/old"> Old </a>

And chains goes on.

Now what I want is every url has one template to render. I want to have one configration as:-

`$routeProvider(when('/albums/:albumName),
 {
     templateUrl : 'albumsList.html',
     controller : 'albumsCtrl'
 })`

I want to specify that whateveer follows /albums/* , using $routeParams collect that value in albumName.

In my scenario,I don't see any need to need to use ui-routes.

You need to use * in $routeProvider that will match all the characters in the url, when : is used it will match all characters till / .

Change your code

$routeProvider(
when('/albums/:albumName),
 {
     templateUrl : 'albumsList.html',
     controller : 'albumsCtrl'
 })

To

$routeProvider(
when('/albums/:albumName*),
 {
     templateUrl : 'albumsList.html',
     controller : 'albumsCtrl'
 })

Reference: https://docs.angularjs.org/api/ngRoute/provider/ $routeProvider

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM