几天前我遇到了同样的问题。经过一番研究,我找到了一个可能对您有所帮助的解决方案:
状态配置
angular.module('app', [
'ui.router'
])
.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('home'); // For any unmatched url, redirect to home.
$stateProvider
.state('home', {
url: '/home',
templateUrl: "", // First template.
controller: "homeController"
})
.state('home.child1', {
url: '/child1',
templateUrl: "", // Second template.
controller: "homeController"
})
.state('home.child2', {
url: '/chil2',
templateUrl: "", // Third template.
controller: "homeController"
})
.state('home.childn', {
url: '/chiln',
templateUrl: "", // n template.
controller: "homeController"
});
}])
控制器
.controller('homeController', ['$scope', '$state', function($scope, $state) {
$scope.active = function(route) {
return $state.is(route);
};
$scope.tabs = [
{ heading: "Home", route:"home", active:false }, // Tab 1
{ heading: "Child One", route:"home.child1", active:false }, // Tab 2
{ heading: "Child Two", route:"home.child2", active:false }, // Tab 3
{ heading: "Child n", route:"home.childn", active:false } // Tab n
];
$scope.$on("$stateChangeSuccess", function() { // Keep the right tab highlighted if the URL changes.
$scope.tabs.forEach(function(tab) {
tab.active = $scope.active(tab.route);
});
});
}]);
最后但同样重要的是,HTML
<tabset>
<tab
ng-repeat="tab in tabs"
heading="{{ tab.heading }}"
ui-sref="{{ tab.route }}"
active="tab.active">
</tab>
</tabset>
这是演示。