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代码同上。