Shamoon
Shamoon

Reputation: 43491

AngularJS Routing based on param

I'm using the $routeProvider and I want the route to load a different view depending on what is passed:

when('/tx/:txid', {
  templateUrl: '/views/tx/view.html'
}).

If txid matches a certain pattern, it should load one view. If it matches a different pattern, it should load a different view. Is this possible?

Upvotes: 0

Views: 283

Answers (2)

hjgraca
hjgraca

Reputation: 1714

Have a look at dynamic templates.

when('/tx/:txid', 
    {   
      controller:ctrl, 
      templateUrl: function(params){ 
         // your logic here...
         return '/tx/diferentview/' + params.txid; 
      }
    }

This should do the trick.

Upvotes: 1

dfsq
dfsq

Reputation: 193261

You can use function for dynamic views:

.when('/tx/:txid', {
    templateUrl: function(routeParams) {
        // return necessary view based on route params
        return '/views/tx/view.html'; 
    }
})

From documentation:

If templateUrl is a function, it will be called with the following parameters: {Array.} - route parameters extracted from the current $location.path() by applying the current route

Upvotes: 1

Related Questions