Javascript Angularjs-侦听列表事件
我已经为我的可排序列表“借用”了这个指令代码,但我不确定当一个列表项被拖到新位置时如何监听Javascript Angularjs-侦听列表事件,javascript,angularjs,Javascript,Angularjs,我已经为我的可排序列表“借用”了这个指令代码,但我不确定当一个列表项被拖到新位置时如何监听 app .directive('sortable', ['$timeout', function($timeout) { return { scope: { ngModel: '=', ngChange: '&' }, link: function(scope, element, attrs)
app
.directive('sortable', ['$timeout', function($timeout) {
return {
scope: {
ngModel: '=',
ngChange: '&'
},
link: function(scope, element, attrs) {
var toUpdate;
var startIndex = -1;
element.sortable({
axis: "y",
revert: true,
start: function (event, ui) {
// on start we define where the item is dragged from
startIndex = $(ui.item).index();
},
stop: function (event, ui) {
// on stop we determine the new index of the
// item and store it there
var newIndex = $(ui.item).index();
var toMove = toUpdate[startIndex];
toUpdate.splice(startIndex, 1);
toUpdate.splice(newIndex, 0, toMove);
$timeout(function() {
scope.ngChange();
});
}
});
scope.$watch('ngModel', function() {
$timeout(function() {
toUpdate = scope.ngModel;
});
});
}
}
}]);
如何在控制器中调用函数?非常感谢您的帮助。该指令接受ngChange,因此要获得通知,您可以使用:
<sortable ng-change="onItemDragged()"></sortable>
有关ng变更指令的更多信息,请参见此处:
$scope.onItemDragged = function() {
// do something
}