angular.js - angular 如何用指令給給清單動態進行class切換
天蓬老师
天蓬老师 2017-05-15 17:02:38
0
2
518

代码

想要的效果是,為目前點擊的那個添加樣式,其他的清空。

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回覆(2)
某草草

不要這麼麻煩了,咱們直接用ng-class吧,像這樣:

修正:

好吧,我錯了

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
    <ul>
        <li ng-repeat="x in ulList" ng-class="{active: currentIndex === $index}" ng-click="handleActive($index)">{{ x.li }}</li>
    </ul>
    <script type="text/javascript">
        var app = angular.module('myApp', []);
        app.controller('myCtrl', ['$scope', function($scope){
            $scope.handleActive = function(index){
              $scope.currentIndex = index;
            };
            
            $scope.ulList = [
                {
                    id: '1',
                    li: '01'
                },{
                    id: '2',
                    li: '02'
                },{
                    id: '3',
                    li: '03'
                },{
                    id: '4',
                    li: '04'
                }
            ];
        }]);
    </script>
</body>
</html>
伊谢尔伦

用屬性綁定的方式
給 ulList 裡的每個元素加個 isSelected 屬性,當 Click 時更改 isSelected 的值
HTML 中用 ng-class

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板