Angular-UI Tabs: add class to a specific tab

﹥>﹥吖頭↗ 提交于 2019-12-03 17:18:11

问题


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:

.nav-tabs > li.new > 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:

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

JS:
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: tab.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: tab.new} {active: active, disabled: disabled}"

Here's the Plunkr.


回答1:


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




回答2:


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>



回答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:

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




回答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.

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');
}



回答5:


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;
}


来源:https://stackoverflow.com/questions/19393559/angular-ui-tabs-add-class-to-a-specific-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!