Reputation: 6543
Consider a certain route let's say myapp\profile which has two modes (buyer/seller) What i would like to achieve is:
What i already considered:
Could you share what is a clean way of doing this?
Upvotes: 1
Views: 134
Reputation: 6813
Normally, in order to dynamically select a template for a state, you can use a function:
state = {
.....
templateUrl: function($stateParams){
if ($stateParams.isThis === true)
return 'this.html'
else
return 'that.html'
}
}
Unfortunately you can't pass other injectables to the templateUrl function. UI.Router only passes $stateParams
. You don't want to alter the URL in anyway so you can't use this.
But you can leverage templateProvider
instead. With this feature, you can pass a service to your templateProvider
function to determine if your user is a buyer or seller. You'll also want to use UI.Router's $templateFactory
service to easily pull and cache your template.
state = {
....
templateProvider: function($templateFactory, profileService){
var url = profileService.isBuyer ? 'buyer.html' : 'seller.html';
return $templateFactory.fromUrl(url);
}
}
Here it is working in your plunkr - http://plnkr.co/edit/0gLBJlQrNPUNtkqWNrZm?p=preview
Docs:
Upvotes: 1