切换<a> class with Angular.js</a>

时间:2013-07-16 18:41:14

标签: angularjs

我正在使用Angular.js创建一个简单的选项卡式界面,但我想在我刚刚点击的链接上切换active类。这是我的代码

    <header>
    <nav>
    <a href ng:click="selected=1">Home</a>
    <a href ng:click="selected=2">About</a>
    <a href ng:click="selected=3">Work</a>
    <a href ng:click="selected=4">Blog</a>
    <a href ng:click="selected=5">Login</a>
    </nav>
    </header>

    <article ng:hide="selected == 2 || selected == 3 || selected == 4 || selected == 5" >
    <section>
    <h2>1</h2>
    </section>
    </article>

    <article ng:show="selected == 2">
    <section>
    <h2>2</h2>
    </section>
    </article>


    <article ng:show="selected == 3">
    <section>
    <h2>3</h2>
    </section>
    </article>


    <article ng:show="selected == 4">
    <section>
    <h2>4</h2>
    </section>
    </article>


    <article ng:show="selected == 5">
    <section>
    <h2>5</h2>
    </section>
    </article>

I also made a CodePen。 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:4)

使用ng-class指令:

<header>
        <nav>
            <a href ng:click="selected=1" ng-class="{active: selected == 1}">Home</a>
            <a href ng:click="selected=2" ng-class="{active: selected == 2}">About</a>
            <a href ng:click="selected=3" ng-class="{active: selected == 3}">Work</a>
            <a href ng:click="selected=4" ng-class="{active: selected == 4}">Blog</a>
            <a href ng:click="selected=5" ng-class="{active: selected == 5}">Login</a>
        </nav>
</header>