Dynamic Responsive Angular-ui bootstrap tabs
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.4/ui-bootstrap-tpls.js"></script>
<div id="demo">
<div ng-controller="AppCtrl">
<p>The media query is set to 400px for a break point, just resize the result pane to see it change from
tabs to an accordion.</p>
<tabset>
<tab heading="tab1">Tab 1 content<br />
<p>Just to show that all the Angular-ness still works, change this field to see that
it is bound to a field in the second tab.</p>
<input type="text" ng-model="model.foo" />
<br />
<br />
<br />
<br />
<br />
</tab>
<tab heading="tab2">Tab 2 content
<br />
<input type="text" ng-model="model.foo" />
<br />
<input type="text" ng-model="model.bar" />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</tab>
</tabset>
</div>
</div>
CSS
div#demo {
padding:10px;
}
.tab-accordion-header, .tab-accordion-header.active {
display:none;
}
@media(max-width: 400px) {
ul.nav-tabs {
display:none;
}
.tab-accordion-header {
background-color: #666666;
color: #ffffff;
padding: 10px;
width: 100%;
margin: 1px 0;
display:block;
}
.tab-accordion-header.active {
display:block;
background-color:#dd3333;
}
.tab-content {
border-top: 0px transparent none;
}
/**
Add this to make them always open
.tab-content > .tab-pane {
display:block;
}
*/
}
JavaScript
angular.module("demo", ['ui.bootstrap'])
.run(function ($templateCache) {
$templateCache.put("template/tabs/tabset.html",
"\n" +
"<div>\n" +
" <ul class=\"nav nav-{{type || 'tabs'}}\" ng-class=\"{'nav-stacked': vertical, 'nav-justified': justified}\" ng-transclude></ul>\n" +
" <div class=\"tab-content\">\n" +
//This is the part that needs to be added. NOTE that the ng-repeat was removed from the original div
//and replaced with the ng-repeat-start and ng-repeat-end directives. This is you aren't limited to
//outputting a single dom node for each tab. We need two, one for the accordion header and one for the tab itself
" <div ng-repeat-start=\"tab in tabs\" ng-click=\"tab.active=true\" class=\"tab-accordion-header\" ng-class=\"{'active': tab.active}\">{{tab.heading}}</div>\n" +
" <div class=\"tab-pane\" \n" +
" ng-repeat-end \n" +
" ng-class=\"{active: tab.active}\"\n" +
" tab-content-transclude=\"tab\">\n" +
" </div>\n" +
" </div>\n" +
"</div>\n" +
"");
})
.controller("AppCtrl", function($scope) {
$scope.model = {foo: "foo", bar:"bar"};
});
;
angular.bootstrap(document.getElementById("demo"), ['demo']);