javascript - $digest 错误,没有模型更改

标签 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)"

最佳答案

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

          return {
                x: 0,
                y: 0
            };

使用 $scope.origin 类型的引用变量,该变量仅在 $scope.getCommentLocation 之外定义一次。 $scope.origin ={x:0, y:0};//永远不要在任何地方重新创建它,只使用原始引用

并使用return $scope.origin

关于javascript - $digest 错误,没有模型更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24068894/

相关文章:

javascript - Discord.js 查找用户创建时间

javascript - Typescript 转译是否处理从 ES6 到 ES5 的转译?

javascript - 优化结果页优先查询结果条数

javascript - 如何处理表格: &lt;input&gt; and <select> in Cordova/Phonegap

angularjs - 向 AngularJS 模型中数组的每个元素添加计算字段

javascript - 如何在 AngularJS 中预取图像?

javascript - 有没有办法检测 Javascript 中的 DOM 节点更改?

javascript - jQuery UI 可调整大小的火窗口调整大小事件

javascript - 无法读取属性对象 reactJS

javascript - 为什么需要一个额外的对象?