【发布时间】:2023-03-27 20:18:01
【问题描述】:
我在我的项目中使用Bootstrap's Tabs。单击选项卡后,我希望更改 url,如下所示:
/home/first
/home/second
但我不知道如何解决它。这是我的$routeProvider 代码:
$routeProvider
.when('/:appId/home', {
templateUrl: 'app/home/home.html',
controller: 'homeController'
})
.when('/:appId/home/first', {
templateUrl: 'app/home/first/first.html',
controller: 'firstController'
})
UPD 指令代码:
'use strict';
angular.module('bootstrap.tabset', [])
.directive('tabset', function () {
return {
restrict: 'E',
replace: true,
transclude: true,
controller: function($scope) {
$scope.templateUrl = '';
var tabs = $scope.tabs = [];
var controller = this;
this.selectTab = function (tab) {
angular.forEach(tabs, function (tab) {
tab.selected = false;
});
tab.selected = true;
};
this.setTabTemplate = function (templateUrl) {
$scope.templateUrl = templateUrl;
};
this.addTab = function (tab) {
if (tabs.length == 0) {
controller.selectTab(tab);
}
tabs.push(tab);
};
},
template:
'<div class="row-fluid">' +
'<div class="row-fluid">' +
'<div class="nav nav-tabs" ng-transclude></div>' +
'</div>' +
'<div class="row-fluid">' +
'<ng-include src="templateUrl">' +
'</ng-include></div>' +
'</div>'
};
})
.directive('tab', function () {
return {
restrict: 'E',
replace: true,
require: '^tabset',
scope: {
title: '@',
templateUrl: '@'
},
link: function(scope, element, attrs, tabsetController) {
tabsetController.addTab(scope);
scope.select = function () {
tabsetController.selectTab(scope);
}
scope.$watch('selected', function () {
if (scope.selected) {
tabsetController.setTabTemplate(scope.templateUrl);
}
});
},
template:
'<li ng-class="{active: selected}">' +
'<a href="" ng-click="select()">{{ title }}</a>' +
'</li>'
};
});
主页 HTML:
<tabset>
<tab title="Tab 1" template-url="/app/home/first/first.html"></tab>
<tab title="Tab 1" template-url="/app/home/home.html"></tab>
</tabset>
app.js
angular.module('frontend', ['ngRoute', 'ui.bootstrap', 'localytics.directives'])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'app/main/main.html',
controller: 'MainCtrl'
})
.when('/:appId/home', {
templateUrl: 'app/home/home.html',
controller: 'homeController'
})
.when('/:appId/home/first', {
templateUrl: 'app/home/first/first.html',
controller: 'firstController'
})
.otherwise({
redirectTo: '/'
});
})
【问题讨论】:
-
如果您可以发布更多代码,最好是工作演示,这将更容易帮助您解决问题。
-
@atefth 我更新了我的问题
-
您能否也发布您的 app.js 和控制器代码?
-
@atefth 我再次更新了我的问题
-
@atefth 我找到了好的组件github.com/rpocklin/ui-router-tabs
标签: javascript jquery angularjs twitter-bootstrap tabs