I have this state structure:
.state('places',
{
url:'/places',
controller:'PlacesController',
templateUrl:'views/places.html'
})
.state('places.city',
{
url:'/:city',
templateUrl:function(stateParams)
{
return 'views/places/'+stateParams.city+'.html';
}
});
It works nicely except when the URL is simply /places
without a city after it.
How can I have a default value for templateUrl
in the child state?
I'm thinking the best way would be to set the ui-view from PlacesController
, however that doesn't seem to be possible.
Use resolve on the parent state
Check for syntax error, I'm used to coffeescript :)
Without needing any change in your states configuration, you can put your default template directly between the
ui-view
tags in your HTML:Now when you navigate to
/places
, you will see the "Please choose a place" message, and when you navigate to/places/london
, you will see "London is amazing!".As stated in the Angular-UI Wiki page:
https://github.com/angular-ui/ui-router/wiki#activating-a-state
as stated by Aaron you can use $state to navigate to a default child state but here there is a catch say suppose you have more than 1 child :
and in you parent controller PlacesController you have $state.go(places.city) then whenever the parent controller i.e. PlacesController is invoked you will be redirected to city state. So if someone is on child2 state and presses a refresh button then parent controller will be invoked and he will be redirected to city which is not what you want!
ANS: So you can check for the current state before doing a redirection i.e. if the person is in parent state then redirect to any default child state but if the person is in any of the child states then do not do the redirection in the parent controller :-
hope this helps!
Try defining another child state with the URL set to an empty string:
Then add
abstract: true
to the parent state.