$ digest错误,没有模型更改

时间:2014-06-05 19:42:56

标签: javascript angularjs

我在Angularjs中收到以下错误:

"Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: parentValueWatch; newVal: {\"x\":0,\"y\":0}; oldVal: {\"x\":0,\"y\":0}"],["fn: parentValueWatch; newVal: {\"x\":0,\"y\":0}; oldVal: {\"x\":0,\"y\":0}"],["fn: parentValueWatch; newVal: {\"x\":0,\"y\":0}; oldVal: {\"x\":0,\"y\":0}"],["fn: parentValueWatch; newVal: {\"x\":0,\"y\":0}; oldVal: {\"x\":0,\"y\":0}"],["fn: parentValueWatch; newVal: {\"x\":0,\"y\":0}; oldVal: {\"x\":0,\"y\":0}"]]

newValoldVal日志完全相同。根据How to Troubleshoot Angular "10 $digest() iterations reached" Error,当值发生变化时,应该抛出错误。

这是我的应用代码

$scope.getCommentLocation = function(comment) {
        console.log("TEST");
        if (comment == undefined) { // This is all that gets returned every time
            return {
                x: 0,
                y: 0
            };
        }
        $range = getRangeObjectForComment(comment);
        position = $range.offset();
        window_position_y = position.top - $('#main-text_ifr').contents().find('body').scrollTop() + frameposition.top + $range.height() / 2;
        window_position_x = position.left + frameposition.left + $range.width() / 2;
        return {
            x: window_position_x,
            y: window_position_y
        };
    };

这是 jade代码(Jade是一种html模板语言):

location="getCommentLocation(commentEditor.activeComment)"

1 个答案:

答案 0 :(得分:1)

以下代码将始终创建一个新对象并将其返回。

          return {
                x: 0,
                y: 0
            };

使用引用变量类$ scope.origin,它只会在$scope.getCommentLocation之外定义一次。 $scope.origin ={x:0, y:0};//never recreate this any where just use the origin referrence

并使用return $scope.origin