我在这里创建了Sample For Tabs。
我真正需要的是手动内部样本控制器我需要根据配置参数设置选定的选项卡。在加载时我需要手动设置要选择的选项卡(基于tabid也没关系)。我需要在controller.can中使用此功能。任何人都可以帮助我
angular.module('components', []).
directive('tabs', function() {
return {
restrict: 'E',
transclude: true,
scope: {},
controller: [ "$scope", function($scope) {
var panes = $scope.panes = [];
$scope.select = function(pane) {
angular.forEach(panes, function(pane) {
pane.selected = false;
});
pane.selected = true;
}
this.addPane = function(pane) {
if (panes.length == 0) $scope.select(pane);
panes.push(pane);
}
}],
template:
'<div class="tabbable">' +
'<ul class="nav nav-tabs">' +
'<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">'+
'<a href="" ng-click="select(pane)">{{pane.title}}</a>' +
'</li>' +
'</ul>' +
'<div class="tab-content" ng-transclude></div>' +
'</div>',
replace: true
};
}).
directive('pane', function() {
return {
require: '^tabs',
restrict: 'E',
transclude: true,
scope: { title: '@' },
link: function(scope, element, attrs, tabsCtrl) {
tabsCtrl.addPane(scope);
},
template:
'<div class="tab-pane" ng-class="{active: selected}" ng-transclude>' +
'</div>',
replace: true
};
})
.controller('sample', function($scope){
//Here I need to Change Selected Tab
})
答案 0 :(得分:1)
Try this它会解决您的问题
将指令范围用作
scope: {
selected : '='
},
将视图中的值传递为
<tabs data-selected="3">
然后在你的指令addPane funstion as
this.addPane = function(pane) {
if (panes.length == $scope.selected) $scope.select(pane);
panes.push(pane);
}
如果您将范围变量作为
,则可以从控制器进行配置controller('sample', function($scope){
$scope.selectedTab = 3;
}
将此变量公开为
<tabs data-selected="selectedTab">
如果你想要它具有pan id,那么在pane指令上使用scope variable作为
scope: { title: '@', id:'=' },
并将您的addPane方法更新为
this.addPane = function(pane) {
if (pane.id == $scope.selected) $scope.select(pane);
panes.push(pane);
}
还在您的窗格上添加了一些ID
<pane id="1" title="First Tab">
答案 1 :(得分:1)
试试这个plunker。
这似乎是同样的解决方案,由@Partha Sarathi Ghosh提供。
在我的情况下,我将手表添加到selected
。您也可以在运行时更改selected
。
angular.module('components', []).
directive('tabs', function() {
return {
restrict: 'E',
transclude: true,
scope: {selected:"="},
controller: [ "$scope", function($scope) {
var panes = $scope.panes = [];
$scope.select = function(pane,ind) {
angular.forEach(panes, function(pane) {
pane.selected = false;
});
if(ind!=undefined)
$scope.selected = ind;
pane.selected = true;
}
$scope.$watch('selected',function(newVal){
var pane = $scope.panes[newVal];
console.log(newVal)
if(pane)
$scope.select(pane);
})
this.addPane = function(pane) {
panes.push(pane);
if (panes.length == $scope.selected+1) $scope.select(pane);
}
}],
template:
'<div class="tabbable">' +
'<ul class="nav nav-tabs">' +
'<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">'+
'<a href="" ng-click="select(pane,$index)">{{pane.title}}</a>' +
'</li>' +
'</ul>' +
'<div class="tab-content" ng-transclude></div>' +
'</div>',
replace: true
};
}).
directive('pane', function() {
return {
require: '^tabs',
restrict: 'E',
transclude: true,
scope: { title: '@' },
link: function(scope, element, attrs, tabsCtrl) {
tabsCtrl.addPane(scope);
},
template:
'<div class="tab-pane" ng-class="{active: selected}" ng-transclude>' +
'</div>',
replace: true
};
})
.controller('sample', function($scope){
//Here I need to Change Selected Tab
$scope.selected = 2;
})
和HTML
<body ng-app="components" ng-controller="sample">
<h3>BootStrap Tab Component</h3>
<input ng-model="selected">
<tabs selected="selected">
<pane title="First Tab" selected="true">
<div>This is the content of the first tab.</div>
</pane>
<pane title="Second Tab">
<div>This is the content of the second tab.</div>
</pane>
<pane title="Third Tab" selected="true">
<div>This is the content of the Third tab.</div>
</pane>
<pane title="Fourth Tab">
<div>This is the content of the Fourth tab.</div>
</pane>
</tabs>
</body>