angular js实现开关效果


功能:实现点击排序,再点击排倒序。

实现方法如下

方法一:定义变量实现点击切换true或false,代码为:

         $scope.lidata = [
                {"name":"Terry","age":12},
                {"name":"Jenifer","age":45},
                {"name":"Garry","age":36},
                {"name":"Tao","age":24},
                {"name":"Lee","age":34},
         ];
     $scope.sortTmp = false;    $scope.sortFn = function(arg){   $scope.sortTmp = !$scope.sortTmp; //在这实现点击的切换   $scope.lidata = $filter('orderBy')($scope.lidata, arg, $scope.sortTmp); }

其中对应的html代码为:

        <table style="margin-left:20px">
            <tr>
                <th ng-click = "sortFn('name')">姓名</th>
                <th ng-click = "sortFn('age')">年龄</th>
            </tr>
            <tr ng-repeat = "data in lidata">
                <td>{{data.name}}</td>
                <td>{{data.age}}</td>
            </tr>
        </table>

方法二:函数也是对象,可以赋属性。

        $scope.sortFn = function(arg){
            arguments.callee["sortFn" + arg] = !arguments.callee["sortFn" + arg]
            $scope.lidata = $filter('orderBy')($scope.lidata,arg,arguments.callee["sortFn" + arg]);
        }    

html代码同上。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM