Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 8285e2d

Browse files
author
Robert Messerle
committed
feat(tabs): adds support for md-autoselect
1 parent 4af2813 commit 8285e2d

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/components/tabs/demoDynamicTabs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div ng-controller="AppCtrl" class="sample" layout="column">
22
<md-content class="md-padding">
3-
<md-tabs md-selected="selectedIndex" md-border-bottom>
3+
<md-tabs md-selected="selectedIndex" md-border-bottom md-autoselect>
44
<md-tab ng-repeat="tab in tabs"
55
ng-disabled="tab.disabled"
66
label="{{tab.title}}">

src/components/tabs/js/tabsController.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
1111
locked = false,
1212
elements = getElements(),
1313
queue = [],
14-
destroyed = false;
14+
destroyed = false,
15+
loaded = false;
1516

1617
ctrl.scope = $scope;
1718
ctrl.parent = $scope.$parent;
@@ -51,8 +52,11 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
5152
$scope.$watch('$mdTabsCtrl.hasContent', handleHasContent);
5253
angular.element($window).on('resize', handleWindowResize);
5354
angular.element(elements.paging).on('DOMSubtreeModified', ctrl.updateInkBarStyles);
54-
$timeout(updateHeightFromContent, 0, false);
55-
$timeout(adjustOffset);
55+
$timeout(function () {
56+
updateHeightFromContent();
57+
adjustOffset();
58+
loaded = true;
59+
});
5660
$scope.$on('$destroy', cleanup);
5761
}
5862

@@ -88,6 +92,7 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
8892
ctrl.lastSelectedIndex = oldValue;
8993
ctrl.updateInkBarStyles();
9094
updateHeightFromContent();
95+
adjustOffset();
9196
$scope.$broadcast('$mdTabsChanged');
9297
ctrl.tabs[oldValue] && ctrl.tabs[oldValue].scope.deselect();
9398
ctrl.tabs[newValue] && ctrl.tabs[newValue].scope.select();
@@ -208,6 +213,8 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
208213
}
209214
processQueue();
210215
updateHasContent();
216+
//-- if autoselect is enabled, select the newly added tab
217+
if (loaded && $scope.autoselect) $timeout(function () { select(ctrl.tabs.indexOf(tab)); });
211218
return tab;
212219
}
213220

src/components/tabs/js/tabsDirective.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @param {boolean=} md-no-pagination When enabled, pagination will remain off
6262
* @param {boolean=} md-swipe-content When enabled, swipe gestures will be enabled for the content area to jump between tabs
6363
* @param {boolean=} md-no-disconnect If your tab content has background tasks (ie. event listeners), you will want to include this to prevent the scope from being disconnected
64+
* @param {boolean=} md-autoselect When present, any tabs added after the initial load will be automatically selected
6465
*
6566
* @usage
6667
* <hljs lang="html">
@@ -96,7 +97,8 @@ function MdTabs ($mdTheming, $mdUtil, $compile) {
9697
selectedIndex: '=?mdSelected',
9798
stretchTabs: '@?mdStretchTabs',
9899
swipeContent: '=?mdSwipeContent',
99-
noDisconnect: '=?mdNoDisconnect'
100+
noDisconnect: '=?mdNoDisconnect',
101+
autoselect: '=?mdAutoselect'
100102
},
101103
template: function (element, attr) {
102104
attr["$mdTabsTemplate"] = element.html();

0 commit comments

Comments
 (0)