Each instance of your directive should have its own header, sort type, and reverse property. Thus, the directive will need its own (child) scope - either the selection area ( scope: {} ) or a new area ( scope: true ). Since the directive is not a standalone / standalone component, I would not use a selection area (see Also When writing a directive in AngularJS, how can I decide if I need a new area, a new child scope, or a new isolated volume?. )
With the type of region selected for the directive, the sort type and return values ββcan be passed to the parent through function arguments or they can be set directly in the parent region. I suggest function arguments:
app.directive('headerSort', function () { return { scope: true,
function MyCtrl($scope) { $scope.orderField = "FirstName"; $scope.reverse = false; $scope.customers = [ {FirstName: 'Martijn', Age: 22}, {FirstName: 'Mark', Age: 44}]; $scope.doSort = function (sortType, reverse) { console.log('sorting',sortType, reverse); $scope.orderField = sortType; $scope.reverse = reverse; }; }
<table> <th data-header-sort="FirstName" data-caption="First name"></th> <th data-header-sort="Age" data-caption="Age"></th> <tbody id="customerRows" data-ng-repeat="customer in customers | orderBy:orderField:reverse"> <tr><td>{{customer.FirstName}}<td>{{customer.Age}} </tbody> </table>
fiddle In the fiddle, just for simplicity, I did not include the FullName column.
source share