0

Here I created Sample For Tabs.

What I exactly need is inside sample controller manually I need to set selected tab based on config parameter. At load time I need to set manually the tab to be selected(Based on tabid is also fine). I need this functionality inside controller.can any one help me on this

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
  })
Partha Sarathi Ghosh
  • 10,936
  • 20
  • 59
  • 84
Suresh B
  • 1,658
  • 1
  • 17
  • 35

2 Answers2

1

Try this it will solve your problem

Use your directive scope as

scope: {
  selected : '='
},

Pass value from view as

<tabs data-selected="3">

Then in your directive addPane funstion as

this.addPane = function(pane) {
   if (panes.length == $scope.selected) $scope.select(pane);
      panes.push(pane);
}

You can configure it from controller if you take a scope variable there as

controller('sample', function($scope){
      $scope.selectedTab = 3;
}

Expose this variable to view as

 <tabs data-selected="selectedTab">

If you want it with pan id then use scope variable on pane directive as

scope: { title: '@', id:'=' },

And update your addPane method as

this.addPane = function(pane) {
 if (pane.id == $scope.selected) $scope.select(pane);
      panes.push(pane);
 }

Also put some id on your panes as

 <pane id="1" title="First Tab">
Partha Sarathi Ghosh
  • 10,936
  • 20
  • 59
  • 84
1

Try this plunker.

This is seems same solution, has a provided by @Partha Sarathi Ghosh.

In my case i'm add watch to the selected. You can change selected in run-time too.

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;
})

And 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>
Stepan Kasyanenko
  • 3,176
  • 1
  • 15
  • 23