【问题标题】:Accessing the state of AngularUI tab from within the elements it contains从其包含的元素中访问 AngularUI 选项卡的状态
【发布时间】:2014-08-01 10:22:11
【问题描述】:

我使用 AngularUI 构建了以 Bootstrap 为主题的标签:

<tabset>
    <tab>
        <tab-heading>Javascript <span class="badge">{{ count }}</span></tab-heading>
    </tab>
    <tab>
        <tab-heading>Python <span class="badge">{{ count }}</span></tab-heading>
    </tab>
    <tab>
        <tab-heading>Ruby <span class="badge">{{ count }}</span></tab-heading>
    </tab>
</tabset>

看到&lt;span&gt;{{ count }} 了吗?我希望它们仅在它们的包含选项卡被选中(活动)时显示。像这样的东西会很完美:

<span ng-show="tab.active" class="badge">{{ count }}</span>

这不起作用,所以我编写了函数$scope.setActiveTab$scope.isTabActive,现在我的代码如下所示:

<tab select="setActiveTab(0)">
    <tab-heading>Javascript <span ng-show="isTabActive(0)" class="badge">{{ count }}</span></tab-heading>
</tab>
<tab select="setActiveTab(1)">
    <tab-heading>Python <span ng-show="isTabActive(1)" class="badge">{{ count }}</span></tab-heading>
</tab>
<tab select="setActiveTab(2)">
    <tab-heading>Ruby <span ng-show="isTabActive(2)" class="badge">{{ count }}</span></tab-heading>
</tab>

这可行,但看起来很难看。 (更不用说这不适用于多个标签集)。

有没有更好的方法从这些范围内访问选项卡的状态?

Plunker 使用我当前的代码:http://plnkr.co/edit/Id3aSU4NPcPNJO6gXuoo

【问题讨论】:

    标签: angularjs angular-ui angular-ui-bootstrap


    【解决方案1】:

    这在ng-repeat 中会更容易一些

    <tab ng-repeat="tab in tabs" active="tab.active">
    ...
    ng-show="tab.active" 
    

    Fiddle

    【讨论】:

    • 谢谢!使用ng-repeat,代码看起来更简洁。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多