Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angularjs-侦听列表事件_Javascript_Angularjs - Fatal编程技术网

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
}