我正在尝试制作一个使用Angular UI Bootstrap选项卡的指令 该指令的主要目的是记住已选择的选项卡 我已经查看了那里的所有解决方案,似乎没有人为控制器中未设置的选项卡解决此问题:
<tabset>
<tab heading="Customers" sctab>Customer tab content</tab>
<tab heading="Sales" sctab>Sales Tab Content</tab>
</tabset>
注意“sctab”是我的指令名称。在这里,我有以下工作:
然而,我在最后一步遇到了障碍:
为此,我尝试了以下代码中可以看到的一些内容。没有完全奏效。
我认为活动状态是在ui bootstrap tabs指令的isolate-scope变量中设置的,称为“active”。
ngClass也绑定到这个变量:
ng-class="{active: active, disabled: disable}"
所以基本上在这里我一直试图为标签设置active = true以试图激活标签(无效)。
在详细介绍之前,我会在这里停下来看看是否有人根据我的尝试提出了各种想法
这是我的指示:
angular.module('jonsmodule')
.directive('sctab', ['$rootScope', '$state', '$location', function($rootScope, $state, $location) {
return {
restrict: "A",
link: function (scope, element, attrs) {
/**
* INITIALIZING THE ACTIVE TAB FROM HASH
*/
scope.$watch(
// watch for setting of the element heading (so we know when the bootstrap tabs are loaded)
function () { return element.children().first('p')[0].innerText; },
function (newValue, oldValue) {
// when ui-bootstrap tabs loaded
if (newValue !== oldValue) {
// if hash matches the tab heading name
if ($location.hash() == newValue.trim()) {
/**
* ******** NEED HELP HERE ************
* THE GOAL HERE IS TO SET THE TAB ACTIVE
* it does not work completely
* below is a list of things I tried
*/
//$(element).tab('show'); // .tab is not a function
$(element).addClass('active'); // changes the class but the tab will not change
scope.$$childHead.active = true; // appears to not save
// attrs.$set('ngClass', {active: true, disabled: false}); // this does not appear to change anything
} else {
scope.$$childHead.active = false; // appears to save and the first tab is not selected
}
}
}
);
/**
* CHANGING THE TAB
*/
// get the state (controller)
var current_state = $state.current.name;
// on tab change set the hash
element.on('click', function () {
var tabLabel = element.children().first('p')[0].innerText;
history.pushState(null, null, current_state + '#' + tabLabel);
});
},
};
}]);
答案 0 :(得分:0)
尝试在选项卡本身中设置选项卡的活动状态,并将其指向范围值。例如:
<tab select="tabSelected('tab1')" active="tabs.tab1">
<tab select="tabSelected('tab2')" active="tabs.tab2">
$scope.tabs = {tab1: false, tab2:false};
$scope.tabSelected = function (whichTab) {
$scope.currentTab = whichTab;
};
function setTabDisplayed() {
$scope.tabs[$scope.currentTab] = true;
}