Reputation: 600
In this Plunker, I'm unable to make menu links and tabs to work properly. As you can see I need to click twice the 'Route 1' to go back from tabs Route2, moreover when I click twice the 'Route 2' menu link, the tabs content is not rendered.
I think this is the relevant part of the code that matters:
myapp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/");
$stateProvider
.state('route1', {
url: "/",
templateUrl: "route1.html"
})
.state('DocumentoMasterView', {
url: "/route2",
templateUrl: "route2.html",
controller: 'myAppController'
})
.state('DocumentoMasterView.A', {
url: '/detail',
templateUrl: 'route2.A.view.html',
controller: 'myAppController'
})
.state('DocumentoMasterView.B', {
url: '/image',
templateUrl: 'route2.B.view.html',
controller: 'myAppController'
})
.state('DocumentoMasterView.C', {
url: '/submenu',
templateUrl: 'route2.C.view.html',
controller: 'myAppController'
})
});
myapp.controller('myAppController',['$scope','$state',function($scope, $state){
$scope.tabs = [
{ heading: 'A View', route:'DocumentoMasterView.A', active:true},
{ heading: 'B View', route:'DocumentoMasterView.B', active:false },
{ heading: 'C View', route:'DocumentoMasterView.C', active:false }
];
$scope.go = function(route){
$state.go(route);
};
$scope.active = function(route){
return $state.is(route);
};
$scope.$on('$stateChangeSuccess', function() {
$scope.tabs.forEach(function(tab) {
tab.active = $scope.active(tab.route);
});
});
Upvotes: 3
Views: 5188
Reputation: 622
Radim's answer is great but this is an outdated/ bloated method. ui-router has active states which do all these css and state changing from the view rather than having the logic in your controller.
In your nav:
<ul>
<li ui-sref-active="active" class="item">
<a href ui-sref="route1">route1</a>
</li>
</ul>
And thats it! Your currently active state/ view will apply the ui-sref-active="active" class to that li element. where "active" is the class name which is applied.
For your specific nav it would look like this:
<ul>
<li ui-sref-active="active" class="item">
<a href ui-sref="route1">route1</a>
</li>
<li ui-sref-active="active" class="item">
<a href ui-sref="DocumentoMasterView">DocumentoMasterView</a>
</li>
<li ui-sref-active="active" class="item">
<a href ui-sref="DocumentoMasterView.A">DocumentoMasterView.A</a>
</li>
<li ui-sref-active="active" class="item">
<a href ui-sref="DocumentoMasterView.B">DocumentoMasterView.B</a>
</li>
<li ui-sref-active="active" class="item">
<a href ui-sref="DocumentoMasterView.C">DocumentoMasterView.C</a>
</li>
</ul>
Upvotes: 1
Reputation: 123861
I've made this change to make that example working (check it here)
we do not need state change in this case
// instead of this
$scope.go = function(route){
$state.go(route);
};
$scope.$on('$stateChangeSuccess', function() {
$scope.tabs.forEach(function(tab) {
tab.active = $scope.active(tab.route);
});
});
we should handle all the on tab selection
// use just this
$scope.go = function(t){
$scope.tabs.forEach(function(tab) {
tab.active = $scope.active(t.route);
});
$state.go(t.route);
};
Check it here
Also, try to reconsider using of the <div ng-controller="myAppController">
with ui-router. It could work, but with states you can define all the parts more effectively.
Here I tried to show how to... no ng-controller, parent layout state...
Upvotes: 3