AngularJS 设置初始活动类

编程入门 行业动态 更新时间:2024-10-07 02:26:54
本文介绍了AngularJS 设置初始活动类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用 引导标签 和 Angular ng-repeat.

我遇到的问题是第一个 li 和第一个 div.tab-pane 都需要一个活动类.Bootstrap 会在一个选项卡被点击后处理这个问题,但是,正如你所看到的,不是在加载时.

我的问题是,如何在第一个 lidiv.tab-pane 上设置活动类?

演示:

http://jsfiddle/FQgHx/

解决方案

您所要做的就是将 ng-class 指令放在您最初希望激活的选项卡和选项卡窗格上.您可以将 ng-class 指令中的 $index == 0 表达式更改为您想要的任何内容.这可以使用您范围内的函数或变量.

这是一个 JS Fiddle,展示了如何做到这一点:http://jsfiddle/digitalzebra/qqPZd/

这是最终代码...注意 ng-class 指令:

<div ng-init="names = [{name:'one'}, {name:'two'}, {name:'three'}, {name:'four'}, {name:'five'}]"><ul class="nav nav-tabs"><li ng-repeat="name in names" ng-class="{active: $index == 0}"><a href="#tab{{$index + 1}}" data-toggle="tab">周{{acute.Week}}</a><div class="tab-content"><div class="tab-pane淡入" id="tab{{$index + 1}}" ng-repeat="name in names" ng-class="{active: $index == 0}"><p>{{$index + 1}}</p>

I'm using Bootstrap tabs with an Angular ng-repeat.

The problem I'm running into is that the first li and the first div.tab-pane each need an active class. Bootstrap takes care of that after a tab is clicked but, as you can see, not on load.

My question is, how can I set an active class on the first li and div.tab-pane?

Demo:

http://jsfiddle/FQgHx/

解决方案

All you have to do is put an ng-class directive on the tab and tab pane that you would like active initially. You can change the expression from $index == 0 in the ng-class directive to whatever you would like. This could use a function or variable in your scope.

Here is a JS Fiddle showing how to do this: http://jsfiddle/digitalzebra/qqPZd/

And here is the final code... notice the ng-class directives:

<div ng-app>
    <div ng-init="names = [{name:'one'}, {name:'two'}, {name:'three'}, {name:'four'}, {name:'five'}]">
    <ul class="nav nav-tabs">
    <li ng-repeat="name in names" ng-class="{active: $index == 0}">
      <a href="#tab{{$index + 1}}" data-toggle="tab">Week {{acute.Week}}</a>
    </li>
  </ul>
  <div class="tab-content">
    <div class="tab-pane fade in" id="tab{{$index + 1}}" ng-repeat="name in names" ng-class="{active: $index == 0}">
      <p>{{$index + 1}}</p>
    </div>
  </div>
</div>
</div>

这篇关于AngularJS 设置初始活动类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 18:28:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1007801.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AngularJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!