在 uib-tab 中添加带有活动和 ng 重复的选项卡



所以我一直在尝试解决这个问题一段时间,我看到一个与此类似但未回答的帖子,所以我会尝试发布这个,但用一个 plunkr 作为示例显示。

所以问题是在加载时,我注意到 ui-tab 始终设置为"添加选项卡"按钮。我想做的是让 ui-tab ng-repeat 中的第一个元素处于活动状态,而不是静态的添加选项卡按钮。

<uib-tabset active="activeTabIndex"> <uib-tab ng-repeat="tab in tabs" heading="{{tab.title}}">Some content</uib-tab> <uib-tab heading="Add a tab" ng-click="addTab()" >Add a tab</uib-tab> </uib-tabset>

https://plnkr.co/edit/XrYSKLdyN1cegfcdjmkz?p=preview

我怎样才能做到这一点?我已经做了一段时间,但仍然不知道如何解决这个问题。

谢谢

猜你可能喜欢这个固定的 plunker

创建自己的directive而不是使用额外的<uib-tab>来实现那里有点棘手。

示例代码:

<!doctype html>
<html ng-app="ui.bootstrap.demo">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
  <script src="https://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.2.4.js"></script>
  <script type="text/javascript">
    angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
    angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function($scope, $window, $timeout) {
      $scope.tabs = [{
        title: 'Tab1',
        content: 'content1'
      }, {
        title: 'Tab2',
        content: 'content2'
      }];
      $scope.activeTabIndex = 0;//$scope.tabs.length - 1;
      $scope.addTab = function() {
        var newTab = {
          title: 'Tab ' + ($scope.tabs.length + 1),
          content: 'content ' + ($scope.tabs.length + 1)
        };
        $scope.tabs.push(newTab);
        $timeout(function() {
          $scope.activeTabIndex = ($scope.tabs.length - 1);
        });
        console.log($scope.activeTabIndex);
      };
    });
    angular.module('ui.bootstrap.demo').directive('uibTabButton', function() {
      return {
        restrict: 'EA',
        scope: {
          handler: '&',
          text:'@'
        },
        template: '<li class="uib-tab nav-item">' +
          '<a href="javascript:;" ng-click="handler()" class="nav-link" ng-bind="text"></a>' +
          '</li>',
        replace: true
      }
    });
  </script>
  <link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div ng-controller="TabsDemoCtrl">
    Active index: {{ activeTabIndex }}
    <br /> Tab count: {{ tabs.length }}
    <br />
    <input type="button" value="Add Tab" ng-click="addTab()" />
    <uib-tabset active="activeTabIndex">
      <uib-tab active="activeTabIndex==$index" ng-repeat="tab in tabs" heading="{{tab.title}}">{{tab.content}}</uib-tab>
      <uib-tab-button handler="addTab()" text="Add a tab"></uib-tab-button>
    </uib-tabset>
  </div>
</body>
</html>

最新更新