如何创建一个监听嵌套属性的$ watch函数?

时间:2016-06-01 15:46:43

标签: javascript jquery angularjs

我有一个函数属性,当我拖动div框(gridster)时会触发该属性。该函数没有问题,因为我每次都可以在console.log()中看到它。但我似乎无法在函数上加$watch。我想在以下对象中调用时startdrag和/或stop属性:

$scope.gridsterOpts = {
    draggable: {
        start: function(event, $element, widget) {console.log('started: ' + JSON.stringify(widget));},  
        drag: function(event, $element, widget) {console.log('moved: ' + JSON.stringify(widget));}, // optional callback fired when item is moved,
        stop: function(event, $element, widget) {logWidget(widget);}  
    }
}

这是我尝试过的:

$scope.$watch("gridsterOpts.draggable.start", function(newValue, oldValue) {
    console.log('gridsterOpts changed')
}, true);

有人可以告诉我实施$ watch服务的正确方法吗?

1 个答案:

答案 0 :(得分:0)

我认为你可以这样做:

$scope.$watch(function() {return $scope.gridsterOpts.draggable.start}, function(newValue, oldValue) {
            console.log('gridsterOpts changed')
        }, true);