我创建了这个jsfiddle:
http://jsfiddle.net/noahgoodrich/CDwfL/1/
我创建了一组指令来管理和操作导航选项卡。出于某种原因,当我尝试关闭选项卡时,它最初会删除适当的数组元素,但随后该元素被替换为空元素。打开选项卡似乎按预期工作。
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)
}
});
}
}
}])
谁能提供有关我如何错误使用的见解navTabsService
?或者是和指令link()
中的功能有关的东西?navTabOpen
navTabClose