简体   繁体   English

Angular-UI选项卡:将类添加到特定选项卡

[英]Angular-UI Tabs: add class to a specific tab

I want to create the following tabs with Angular UI: 我想用Angular UI创建以下选项卡:

标签
(source: gyazo.com ) (来源: gyazo.com

So, I'm adding the styles based on Bootstap's class names/markup: 所以,我正在添加基于Bootstap类名/标记的样式:

.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.) (正如你所看到的,我在添加超文本文本后需要在<a>上补偿填充,否则会被推下来。)

Next, I have the following markup & code in html/js files: 接下来,我在html/js文件中有以下标记和代码:

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. 所以,剩下的就是让Angular根据上面的tabs数组定义在正确的元素上添加一个名为"new"的类。 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}" (在html代码中注释),但这实际上打破了所有类功能,在Dev Tools中查看时产生了错误的ng-class属性:

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

Here's the Plunkr . 这是Plunkr

I'm not sure that you can apply ng-class to tabs like that. 我不确定你是否可以将ng-class应用于这样的标签。 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. 尝试失败后,我决定查看选项卡的bootstrap-ui源,并做出与标题标题属性相关的有趣发现。 Apparently you can put html in the heading section if you place the tab-heading as a child to a tab element. 显然,如果将tab-heading作为子项放置到tab元素,则可以将html放在标题部分中。

Check out the tabheading directive in here . 查看此处的tabheading指令。

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. @TyndieRock是对的,据我所知,你不能将类应用于bootstrap-ui标签。 Any ng-class is stripped and directly adding a class causes bad behavior and it blocks any attempt to inject into it. 任何ng-class都被剥离,直接添加一个类会导致不良行为,并阻止任何注入它的尝试。

I did see an issue posted on github asking for custom css support. 我确实在github上发布了一个问题,要求提供自定义css支持。 So maybe someday. 总有一天也许。

@TyndieRock's solution gets you closer but doesn't get you all the way. @ TyndieRock的解决方案让你更接近,但不会让你一路走来。 Kudos for finding tab-heading . 感谢找到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. 但它对你的css内容效果不佳。

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. 适用于Angular> = 1.1.5,因为它是唯一一个在表达式中具有三元运算符的人。

One needs to target the <tab> element and not tab heading. 需要定位<tab>元素而不是标签标题。 Unfortunatelly ng-class will not work and get mangled together with other ng-class directives from the templates. 不幸的是,ng-class将不起作用,并与模板中的其他ng-class指令一起被破坏。

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. CSS中没有父选择器,因此无法访问大多数操作正在进行的锚标记。

So to have the cookie and eat it: 1) have your styles target descending anchor tag that gets generated by bootstrap tab directive 所以要拥有cookie并吃掉它:1)让你的样式成为由bootstrap tab指令生成的降序锚标记

2) apply the class using this obscure syntax: 2)使用这种模糊的语法应用该类:

 <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. 然后它按预期工作,并将适当的类添加到<li> ,它是有趣的<a>标签的父级。

To sum up: When ng-class is misbehaving and has interpolation issues, try to be more direct. 总结:当ng-class行为不端并且存在插值问题时,请尝试更直接。

hope that helps someone. 希望有人帮助。

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

Try adding an id to the DIV surrounding the tabs and then make your styles based on that. 尝试将ID添加到选项卡周围的DIV,然后根据它创建样式。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM