Eugene Zakharov
Eugene Zakharov

Reputation: 157

How to bind active tab to last element of an array from scope

I'm trying to set uib-tabset active to my last element of an array. However, when I'm doing it uib-tabset is going crazy, it deletes it's own tabs. I don't want to own a scope field for active uib-tabset, I want to use array length to identify which tab should be active. Is it possible?

<uib-tabset active="tabs.length">
    <uib-tab index="$index + 1" ng-repeat="tab in tabs" heading="{{tab.title}}" 
      disable="tab.disabled">
      {{tab.content}}
    </uib-tab>
</uib-tabset>

leads to unexpected results like deleting own tabs. Full example : https://plnkr.co/edit/JJ0k8LtRPXqXKrjEM0bn?p=preview Try to click on first tab.

Upvotes: 0

Views: 596

Answers (3)

Bill P
Bill P

Reputation: 3618

If you don't want another scope variable for the tabs length, this trick should work for you:

<uib-tabset  ng-init="len = tabs.length" active="len">

Upvotes: 0

Bhushan Khaladkar
Bhushan Khaladkar

Reputation: 622

As we need to active last tab initially we can use ng-init for it. Please go through the code hope it will work for you.

<uib-tabset ng-init="active=tabs.length" active="active">
    <uib-tab index="$index + 1" ng-repeat="tab in tabs" heading="{{tab.title}}" 
      disable="tab.disabled">
      {{tab.content}}
    </uib-tab>
  </uib-tabset>

Upvotes: 0

ClaraFrazao
ClaraFrazao

Reputation: 95

I don't know why but saving the tabs' length in the controller seemed to work for me. Hope it helps. Check the plnkr.

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

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function ($scope, $window) {
  $scope.tabs = [
    { title:'Dynamic Title 1', content:'Dynamic content 1' },
    { title:'Dynamic Title 2', content:'Dynamic content 2' },
    { title:'Dynamic Title 3', content:'Dynamic content 3' },
  ];
  $scope.tabslenght = $scope.tabs.length;
  $scope.model = {
    name: 'Tabs'
  };
});
<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <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>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<style type="text/css">
  form.tab-form-demo .tab-pane {
    margin: 20px 20px;
  }
</style>

<div ng-controller="TabsDemoCtrl">


  <uib-tabset active="tabslenght">
    <uib-tab index="$index + 1" ng-repeat="tab in tabs" heading="{{tab.title}}" 
      disable="tab.disabled">
      {{tab.content}}
    </uib-tab>
  </uib-tabset>
</div>
  </body>
</html>

Upvotes: 1

Related Questions