我使用angularjs UI bootstrap制作标签。我在导航栏中有按钮切换到不同的选项卡。带有自己的控制器的导航栏嵌套在主控制器中。我想知道如何在导航栏开关标签中设置按钮。
我有一个here 或者下面的html和js
<div ng-controller="TabsDemoCtrl">
<header>
<div ng-controller="navbarcontroller">
<div class="navbar-header">
<a class="navbar-brand" >Brand
</a>
</div>
<div class="collapse navbar-collapse" ng-class="!navCollapsed && 'in'">
<ul class="nav navbar-nav navbar-right" style="pointer-events: auto;">
<li>
<!-- this buttons dosnt works -->
<button type="button" class="btn btn-default btn-sm" ng-click="active = 1">Select 1st tab</button>
<button type="button" class="btn btn-default btn-sm" ng-click="active = 2">Select 2nd tab</button>
</li>
</ul>
</div>
</div>
</header>
<p> ----- navbar controller ends here ----- </p>
<hr>
<p> ---- tab controller starts here ------ </p>
<!-- this buttons works -->
<button type="button" class="btn btn-default btn-sm" ng-click="active = 1">Select 1st tab</button>
<button type="button" class="btn btn-default btn-sm" ng-click="active = 2">Select 2nd tab</button>
<uib-tabset active="active">
<uib-tab index="1" heading="Tab1" >
Content 1
</uib-tab>
<uib-tab index="2" heading="Tab1" >
Content 2
</uib-tab>
</uib-tabset>
</div>
这是js文件
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo')
.controller('TabsDemoCtrl', function ($scope, $window) {
})
.controller('navbarcontroller', function ($scope, $window) {
});
答案 0 :(得分:1)
您必须在两个控制器之间分享 active
属性。为此,我们需要改变一些事情。
1 - 您必须实现一项服务(让我们称之为tabSelector
),如下所示:
.service('tabSelector', function(){
});
2 - 现在,在TabsDemoCtrl
和navbarcontroller
中,您需要使用以前创建的服务并实现两个使用tabSelector
服务的函数,例如这样:
//this should be done with 'TabsDemoCtrl' and 'navbarcontroller' (this last one was omitted for brevity)
.controller('TabsDemoCtrl', function ($scope, $window, tabSelector) {
//set the active tab
$scope.selectTab = function(tab){
tabSelector.active = tab;
}
//keep synced the active tab
$scope.getActive = function(){
return tabSelector.active;
}
})
3 - 在您看来:
ng-clik
(active = 1
)的内容替换为:selectTab(1)
(控制器中的功能)。请注意,您必须将1
替换为正确的值。<uib-tabset active="active">
<uib-tabset active="getActive()">
内容
请参考this working example (你的分叉者)
您可以在此处找到一些相关信息: