Angular-UI Tabs: Add Class To A Specific Tab


Answer :

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:

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

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.active">     <tab-heading>{{tab.title}} <span ng-show="tab.new">new</span></tab-heading>     <div ng-include="tab.page"></div> </tab> 

@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:

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

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.


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:

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

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.


Comments

Popular posts from this blog

Chemistry - Bond Angles In NH3 And NCl3

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?