对象的值在draggable之外为null

时间:2016-08-04 14:59:44

标签: javascript angularjs jquery-ui-draggable

我有一个使用jquery draggable的函数,只实现了start()和stop()。

我想在stop()内设置一个属性,然后将其检索到OUTSIDE draggable,所以我决定将它添加到窗口中,如下所示:

function dragTimbro() {
    $scope.documentoTimbrabile = true;
    angular.element(document.querySelector('#timbroDraggable'))
                .draggable({
                    containment: "parent",
                    start: function( event, ui ) {
                        $(this).removeClass("draggable");
                        $(this).addClass("dragging");
                    },
                    stop: function( event, ui ) {
                        $(this).addClass("draggable");
                        $(this).removeClass("dragging");
                        $scope.setPos(ui.position.left, ui.position.top);
                    }
                });     
};

$scope.setPos = function(posX, posY) {
    $scope.documentoDaTimbrare[$scope.numPag].posX = posX;
    $scope.documentoDaTimbrare[$scope.numPag].posY = posY;
}



当我尝试访问最后一行中的对象时,它为空 我知道这是一个不同范围的问题,但我不知道如何解决这个问题......

更新:$scope在draggable外定义,因此无法在stop()内访问;

enter image description here



更新2:我知道我会让某人发疯但不知何故我已经设法找到解决方案......我基本上在dragTimbro()之外创建了一个函数然后在stop中调用它。
但我不知道为什么停止看到一个功能而不是一个对象....
我已经更新了代码以使事情变得更清楚

1 个答案:

答案 0 :(得分:-1)

if子句在事件发生之前执行。在这种情况下,将if语句放在stop函数的块中。

在这种情况下,if语句不再有意义,所以我删除了它。顺便说一句,你的标识符很长。

因为在$scope函数的范围内stop对象不再可用,您可以定义全局方法或可在全局对象中访问的属性,您可以在其中更改{ {1}}变量。或者只是将$scope全局。

$scope