我正在尝试定义一个sortable
包装 jqueryui 的可排序插件的指令。
角码是:
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();
};
});
html代码是:
<div ng-controller="MyCtrl">
<ol sortable onStop="updateOrders()">
<li ng-repeat="m in messages">{{m}}</li>
</ol>
</div>
的代码MyCtrl
:
function MyCtrl($scope) {
$scope.updateOrders = function(startIndex, endIndex) {
console.log(startIndex + ", " + endIndex);
}
}
我想在我的回调中获取startIndex
and并对它们做一些事情,但它会打印:endIndex
updateOrders
undefined, undefined
如何将这些参数传递给我的回调?我的方法正确吗?