Angular UI标签 - 单独的标签内容和标签标题

时间:2017-10-13 08:40:10

标签: javascript html angularjs tabs angular-ui-bootstrap

我在尝试使用uib-tabset插件时遇到了一个问题,好吧,插件工作正常,但我需要在特定的东西中使用它。 问题:如何将Tabs标题与标签内容分开。

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function ($scope, $window) { $scope.setActiveTab = function(index){
    $scope.activeTab = index;
  }
});
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.1.3.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div ng-app="ui.bootstrap.demo">
  <div ng-controller="TabsDemoCtrl">
    <div class="some-tabs-header-holder">
      <uib-tabset active="active" vertical="false" >
        <uib-tab index="0" heading="Tab 1"><!-- not needed tab 1 content--></uib-tab>
        <uib-tab index="1" heading="Tab 2"><!-- not needed tab 2 content--></uib-tab>
      </uib-tabset>
    <div>
    <div class="the-tabs-content-holder">
      <div id="tab1" class="tab1-content">tab 1 content</div>
      <div id="tab2" class="tab2-content">tab 2 content</div>
    </div>
  </div>
</div>

我只需要在选中标签时显示“the-tabs-content-holder”div中的内容

2 个答案:

答案 0 :(得分:4)

您可以为每个标签附加select事件处理程序并使用ng-if指令,以便根据tab的值显示$scope.activeTab内容

&#13;
&#13;
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function ($scope, $window) { 
  $scope.activeTab=1;
  $scope.setActiveTab = function(index){
    $scope.activeTab = index;
  }
});
&#13;
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.1.3.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div ng-app="ui.bootstrap.demo">
  <div ng-controller="TabsDemoCtrl">
    <div class="some-tabs-header-holder">
      <uib-tabset active="active" vertical="false" >
        <uib-tab select="setActiveTab(1)" index="0" heading="Tab 1"><!-- not needed tab 1 content--></uib-tab>
        <uib-tab select="setActiveTab(2)" index="1" heading="Tab 2"><!-- not needed tab 2 content--></uib-tab>
      </uib-tabset>
    <div>
    <div class="the-tabs-content-holder">
      <div id="tab1" ng-if="activeTab==1" class="tab1-content">tab 1 content</div>
      <div id="tab2" ng-if="activeTab==2" class="tab2-content">tab 2 content</div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

  • 使用index设置所选标签的ng-click="setActiveTab(0)"
  • 使用$scope.activeTab
  • 根据ng-if的值显示标签内容div

&#13;
&#13;
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function($scope, $window) {
  $scope.activeTab = 0;
  $scope.setActiveTab = function(index) {
    $scope.activeTab = index;
  }
});
&#13;
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.1.3.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div ng-app="ui.bootstrap.demo">
  <div ng-controller="TabsDemoCtrl">
    <div class="some-tabs-header-holder">
      <uib-tabset active="active" vertical="false">
        <uib-tab index="0" heading="Tab 1" ng-click="setActiveTab(0)">
          <!-- not needed tab 1 content-->
        </uib-tab>
        <uib-tab index="1" heading="Tab 2" ng-click="setActiveTab(1)">
          <!-- not needed tab 2 content-->
        </uib-tab>
      </uib-tabset>
      <div>
        <div class="the-tabs-content-holder">
          <div id="tab1" class="tab1-content" ng-if="activeTab == 0">tab 1 content</div>
          <div id="tab2" class="tab2-content" ng-if="activeTab == 1">tab 2 content</div>
        </div>
      </div>
    </div>
&#13;
&#13;
&#13;