0

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?

1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Instead of passing the txid, I can do a check of some kind, like if params.txid is numeric, then return View1 or else View2, right?
you should be able to, yes. in the above example, params.txid can be evaluated independently
Yes you can have any logic you want there, and return the correct view.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.