我正在尝试编写一个指令来处理更改表头的图标类。我想要的是(无论如何我相信)处理按表头排序的标准方法。该指令将添加一个链接元素,并在用户单击时按 desc 排序并将图标更改为 desc,再次单击时按 asc 排序并再次按图标。这是我到目前为止所拥有的,但我现在不知道如何处理图标类以及重置同一张表上但在指令范围之外的其他元素。任何帮助都会很棒!
angular.directive("tableHeaders", function() {
return {
restrict: 'E',
scope: {},
template:'<i class="glyphicon glyphicon-filter"></i>',
link: function(scope, element, attrs) {
attrs.class = 'glyphicon glyphicon-sort-by-alphabet-alt';
}
}
});
这是我在 html 方面的内容:
<th>First Name<a ng-click="newOrderBy('_firstName')"><table-headers></table-headers></a></th>
<th>Last Name<a ng-click="newOrderBy('_lastName')"><table-headers></table-headers></a></th>
<tr ng-repeat="item in items | orderBy:orderBy:reverse>
<td>{{item._firstName}}</td>
<td>{{item._lastName}}</td>
</tr>
order by 当前在控制器中处理:
$scope.newOrderBy = function(order) {
$scope.orderBy = order;
$scope.reverse = !$scope.reverse;
};