Angular-UI Tabs: add class to a specific tab

All we need is an easy explanation of the problem, so here it is.

I want to create the following tabs with Angular UI:

Angular-UI Tabs: add class to a specific tab

So, I’m adding the styles based on Bootstap’s class names/markup:

.nav-tabs > > a {
  padding-top: 5px;
.nav-tabs > .new > a:after {
  content: "NEW";
  color: indianred;
  font-size: 10px;
  vertical-align: super;
  padding-left: 2px;

(As you can see I need to compensate padding on <a> a bit after I added super-scripted text, otherwise it is pushed down.)

Next, I have the following markup & code in html/js files:

    <tab ng-repeat="tab in tabs" heading="{{ tab.title }}" active="" ><!-- ng-class="{new:}"-->
        <div ng-include=""></div>

var TabsDemoCtrl = function ($scope) {
  $scope.tabs = [
    { title:"Tab 1", content:"Dynamic content 1" },
    { title:"Tab 2", content:"Dynamic content 2", active: true, 'new': true }


So, what’s left is making Angular add a class named "new" on the correct element based on tabs array definition above. Unfortunately, I failed to figure out how.

As you can see I tried ng-class="{new:}" (commented in html code), but this essentially breaks all class features, producing incorrect ng-class atrribute when viewing it in Dev Tools:

ng-class="{new:} {active: active, disabled: disabled}"

Here’s the Plunkr.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I’m not sure that you can apply ng-class to tabs like that. After trying and failing I decided to look at the bootstrap-ui source for tabs and made an interesting discovery related to the tab heading attribute. Apparently you can put html in the heading section if you place the tab-heading as a child to a tab element.

Check out the tabheading directive in here.

This is the example they show:

    <tab-heading><b>HTML</b> in my titles?!</tab-heading>
    And some content, too!
    <tab-heading><i class="icon-heart"></i> Icon heading?!?</tab-heading>
    That's right.

In your case I think you might be able to do something like this to get the same effect:

<tab ng-repeat="tab in tabs" active="">
    <tab-heading>{{tab.title}} <span ng-show="">new</span></tab-heading>
    <div ng-include=""></div>

Method 2

@TyndieRock is right that you can’t, as far as I’m aware, apply classes to bootstrap-ui tabs. Any ng-class is stripped and directly adding a class causes bad behavior and it blocks any attempt to inject into it.

I did see an issue posted on github asking for custom css support. So maybe someday.

@TyndieRock’s solution gets you closer but doesn’t get you all the way. Kudos for finding tab-heading. Although his syntax is just slightly off.

Here’s what I think you’d want:

    <tab ng-repeat="tab in tabs">
    <tab-heading ng-class="{}">{{tab.title}}</tab-heading>
        <div ng-include=""></div>

This will let style the text. But it doesn’t work so well for your css content.

You might want to do your own custom tabs. It is a hassle but that’ll give you the control you’re looking for.

Method 3

This issue was driving me mad, so here is what i did to have custom tab heading styling.
Works for Angular >= 1.1.5 as it is the only one to have ternary operator in expressions.

One needs to target the <tab> element and not tab heading. Unfortunatelly ng-class will not work and get mangled together with other ng-class directives from the templates.

If you try to target the tab heading element you will find that there is not much you can do with it. There are no parent selectors in CSS so there is no way to reach that anchor tag where most of the action is going on.

So to have the cookie and eat it:
1) have your styles target descending anchor tag that gets generated by bootstrap tab directive

2) apply the class using this obscure syntax:

        <tab class="{{orderForm.detailsForm.$invalid ? 'invalid-tab' : 'valid-tab'}}">

Then it all works as expected and proper classes are added to the <li> that is a parent to the interesting <a> tag.

To sum up:
When ng-class is misbehaving and has interpolation issues, try to be more direct.

hope that helps someone.

Method 4

I had the same problem today and got around it in a slightly hacky way. Whenever I determined that a specific tab needed a class added/removed from it, I simply used angular selectors to select my tab, and dynamically added/removed classes manually.

<tab class="errorTab" heading='error' active='explorer.hasError'>
  <div error-message class='errorTabSectionOuter'></div>

var errorTab = angular.element('.errorTab');

Method 5

Try adding an id to the DIV surrounding the tabs and then make your styles based on that.
For example

<div id="myTabs">

And here is a style that makes the active tab a different color

#myTabs div a {
  background-color: lightsteelblue;

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply