tmarthal
tmarthal

Reputation: 1528

Create Angular Dynamic Route

I have a route string like the following var global_book_route = /books/:id specified in a variable.

I want to be able to use $route or $location to deep link to this route in a controller, is there a way to do this without re-specifying the url prefix?

This would work: var id=1; $location.path('books/'+id') -> '/books/1'

However, this does not: $location.path(global_book_route).search({id:1}) -> 'books/:id?id=1'

Is there a way I can use the route specified in the string to go to the correct location?

Upvotes: 0

Views: 72

Answers (1)

Jeremy
Jeremy

Reputation: 351

I think you are mixing up the route itself (/books/:id) with the representation of the route in your code.

For example, your global_book_route should be only "/books/".

Then, if you want to load a specific book, you can go the the location global_book_route + book_id as long as the route is declared in your code, like:

 $routeProvider
   .when('/Book/:bookId', {
    templateUrl: 'book.html',
    controller: 'BookController',
    resolve: {        
    }
  })

On a side node, when dealing with routes in Angular, it's really worth it to look into angular-ui, the ui-router offers a way better system to manage your routes and states.

Upvotes: 1

Related Questions