Angular-UI Tabs: add class to a specific tab

后端 未结 5 1340
鱼传尺愫
鱼传尺愫 2021-02-08 13:32

I want to create the following tabs with Angular UI:


(source: gyazo.com)

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

相关标签:
5条回答
  • 2021-02-08 13:45

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

    0 讨论(0)
  • 2021-02-08 13:46

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

    <div id="myTabs">
     <tabset>.....</tabset>
    </div>
    

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

    #myTabs div li.active a {
      background-color: lightsteelblue;
    }
    
    0 讨论(0)
  • 2021-02-08 13:47

    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>
    
    0 讨论(0)
  • 2021-02-08 14:06

    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.

    HTML
    <tab class="errorTab" heading='error' active='explorer.hasError'>
      <div error-message class='errorTabSectionOuter'></div>
    </tab>
    
    JS
    var errorTab = angular.element('.errorTab');
    if($scope.explorer.hasError){
      errorTab.addClass('showErrorTabClass');
      errorTab.removeClass('hideErrorTabClass');
    }
    else{
      errorTab.addClass('hideErrorTabClass');
      errorTab.removeClass('showErrorTabClass');
    }
    
    0 讨论(0)
  • 2021-02-08 14:07

    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.

    0 讨论(0)
提交回复
热议问题