I have created this jsfiddle:
http://jsfiddle.net/noahgoodrich/CDwfL/1/
I have created a set of directives to manage and manipulate navigational tabs. For some reason, when I try to close a tab, it initially removes the appropriate array element, but then the element is replaced with an empty element. Opening tabs seems to work as expected.
app.directive('navTabOpen', ['$parse', 'navTabsService', function($parse, navTabsService) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
scope.tabs = navTabsService.tabs;
element.bind('click', function() {
var tab = null;
var b = scope.$eval(attrs.navTabOpen);
for(var i=0;i<scope.tabs.length;i++) {
scope.tabs[i].active = null;
if(scope.tabs[i].tabId == b.id) {
tab = i;
}
}
if(tab == null) {
tab = {tabId: b.id, name: b.name, stage: b.status, active: 'active'};
scope.tabs.push(tab)
} else {
scope.tabs[tab].active = 'active';
}
});
}
}
}])
.directive('navTabClose', ['$location', 'navTabsService', function($location, navTabsService) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
scope.tabs = navTabsService.tabs;
element.bind('click', function() {
var tab = null;
for(var i=0;i<scope.tabs.length;i++) {
if(scope.tabs[i].tabId == attrs.tabId) {
tab = i;
}
}
if(tab != null) {
if(scope.tabs[tab].active == 'active') {
if(scope.tabs.length == 1) {
$location.path('/');
} else if(tab == 0) {
scope.tabs[1].active = 'active';
} else {
scope.tabs[tab-1].active = 'active';
}
}
scope.tabs.splice(tab,1);
console.log(scope.tabs)
}
});
}
}
}])
Can anyone provide insight into how I'm using the navTabsService
incorrectly? Or is something with the link()
functions in the navTabOpen
and navTabClose
directives?
navTabOpen
andnavTabClose
, but I do see that in thenavTab
directive template you have two directives lying on top of each other in the DOM (<a nav-tab-open><span nav-tab-close></span></a>
), and since they both define a click handler, when you click on thex
both are called successively,nav-tab-close
handler first andnav-tab-open
handler immediately after, and I'm not sure if that's what you intended to do whenx
is clicked. – Stewie 19 hours ago