Pass parameters from directive to callback

I'm trying to define a directive sortable which wraps jqueryui's sortable plugin.

The angular code is:

module.directive('sortable', function () {
    return function (scope, element, attrs) {
        var startIndex, endIndex;
        $(element).sortable({
            start:function (event, ui) {
                startIndex = ui.item.index();
            },
            stop:function (event, ui) {
                endIndex = ui.item.index();
                if(attrs.onStop) {
                    scope.$apply(attrs.onStop, startIndex, endIndex);
                }
            }
        }).disableSelection();
    };
});

The html code is:

<div ng-controller="MyCtrl">
    <ol sortable onStop="updateOrders()">
         <li ng-repeat="m in messages">{{m}}</li>
    </ol>
</div>

The code of MyCtrl:

function MyCtrl($scope) {
    $scope.updateOrders = function(startIndex, endIndex) {
        console.log(startIndex + ", " + endIndex);
    }
}

I want to get the startIndex and endIndex in my callback updateOrders and do something with them, but it prints:

undefined, undefined

How to pass these parameters to my callbacks? Is my approach correct?

This fiddle shows hot callback from a directive passing parameters. Main trick is to use the scope to pass a function. http://jsfiddle.net/pkriens/Mmunz/7/

var myApp = angular.module('myApp', []).
directive('callback', function() {
    return { 
        scope: { callback: '=' },
        restrict: 'A',
        link: function(scope, element) {
            element.bind('click', function() {
                scope.$apply(scope.callback('Hi from directive '));
            })
        }
    };
})

function MyCtrl($scope) {
    $scope.cb = function(msg) {alert(msg);};
}

Then the html looks like for example:

<button callback='cb'>Callback</button>

scope.$apply accepts function or string. In this case using function would be simpler:

  scope.$apply(function(self) {
    self[attrs.onStop](startIndex, endIndex);
  });

Don't forget to change your html code to:

<ol sortable onStop="updateOrders">

(Removed the ())

Alternative 1

If you don't have an isolate scope for this directive, I would use the $parse service for that:

In the controller:

...
$scope.getPage = function(page) {

   ...some code here...

}

In the view:

<div class="pagination" current="6" total="20" paginate-fn="getData(page)"></div>

In the directive:

if (attr.paginateFn) {
   paginateFn = $parse(attr.paginateFn);
   paginateFn(scope, {page: 5})
}

Alternative 2

Now, if you have an isolate scope, you can pass parameters to it as a named map. If your directive is defined like this:

scope: { paginateFn: '&' },

link: function (scope, el) {
   scope.paginateFn({page: 5});
}

taking @Peter Kriens answser a step further you can just check for the name of a on the scope and call it directly.

var myApp = angular.module('myApp', []).
directive('anyOldDirective', function() {
    return { 
        link: function(scope, element) {
            element.bind('click', function() {
                if (scope.wellKnownFunctionName) {
                      scope.wellKnownFunctionName('calling you back!'); 
                } else {
                      console.log("scope does not support the callback method 'wellKnownFunctionName');
                }
            })
        }
    };
})

function MyCtrl($scope) {
    $scope.wellKnownFunctionName= function(a) {console.log(a);};
}