angular.js笔记 ---关于Tabs切换

正在CodeShcool上学习angular.js,记录一点自己觉得可以记录的东西,方便以后查看

<section ng-init="tab = 1" ng-controller="PanelController as panel">
    <ul class="nav nav-pills">
        <li ng-class="{ active:tab === 1}">
            <a href ng-click="tab = 1">Description</a>
        </li>
        <li ng-class="{ active:tab === 2}">
            <a href ng-click="tab = 2">Description</a>
        </li>
        <li ng-class="{ active:tab === 3}">
            <a href ng-click="tab = 3">Description</a>
        </li>
    </ul>
    <div class="panel" ng-show="tab === 1">
        <h4>Description</h4>
        <p>{{product.description}}</p>
    </div>
    <div class="panel" ng-show="tab === 2">
        <h4>Description</h4>
        <p>{{product.description}}</p>
    </div>
    <div class="panel" ng-show="tab === 3">
        <h4>Description</h4>
        <p>{{product.description}}</p>
    </div>
</section>

上面的的代码是angular写的点击切换tab键,还有另一种更简洁的方式如下:

app.js:
app.controller("PanelController",function(){
    this.tab = 1;

    this.selectTab = function(setTab){
        this.tab = setTab;
    }

    this.isSelectTab = function(checkTab){
        return this.tab === checkTab
    }
})
html:
<section ng-init="tab = 1" ng-controller="PanelController as panel">
    <ul class="nav nav-pills">
        <li ng-class="{ active:panel.isSelectTab(1)}">
            <a href ng-click="panel.selectTab(1)">Description</a>
        </li>
        <li ng-class="{ active:panel.isSelectTab(2)}">
            <a href ng-click="panel.selectTab(2)>Description</a>
        </li>
        <li ng-class="{ active:panel.isSelectTab(3)}">
            <a href ng-click="panel.selectTab(3)">Description</a>
        </li>
    </ul>
    <div class="panel" ng-show="panel.isSelectTab(1)">
        <h4>Description</h4>
        <p>{{product.description}}</p>
    </div>
    <div class="panel" ng-show="panel.isSelectTab(2)">
        <h4>Description</h4>
        <p>{{product.description}}</p>
    </div>
    <div class="panel" ng-show="panel.isSelectTab(3)">
        <h4>Description</h4>
        <p>{{product.description}}</p>
    </div>
</section>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值