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
  })
2
  • hope this will help AngularJS controller for a tab Commented Feb 17, 2016 at 5:51
  • on load time itself i need to change. without any click function @Miran Senanayaka Commented Feb 17, 2016 at 6:00

2 Answers 2

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">
Sign up to request clarification or add additional context in comments.

5 Comments

Inside controller I need to set manually ...@Partha Sarathi Ghosh
Here is the pluker with pan id. plnkr.co/edit/fhl6sVFswfukq96Iahdu?p=preview
I think my answer give you step wise update how to achive that.
Sorry. Not possible.
ok no problem... anyway Thanks for ur effort and valueable time @ Partha Sarathi Ghosh
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>

9 Comments

This is bad. I have given this solution on plunker also. But your answer is getting accepted. :(
sorry by mistake i accept that one. don't mine @ Partha Sarathi Ghosh
My solution is better. Because you can change selected in run-time.
is there any way find the element by pane id in angular and active that? @Partha Sarathi Ghosh
for both it's not possible to accept answer but I can vote for both ur effort... thanks @ Stepan Kasyanenko && @ Partha Sarathi Ghosh
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.