AngularJS范围已更新,但UI未更新

时间:2014-02-08 00:53:17

标签: angularjs scope intervals

当页面加载时,我通过设置$ scope.questions并绑定它来在ng-repeat中填充一些内容。然后我每隔几秒钟使用$ interval来查询后端,看是否有任何更新 - 如果我将新数据推送到$ scope.questions。

这会在某些时候更新UI。如果它开始工作,它将继续这样做。大多数时候它肯定会更新$ scope.questions但它没有反映在UI中。它的间歇性使我认为这是一个时间问题,但我无法确定这一点。使用相同的确切操作进行测试不会产生一致的结果。

更新发生在范围内 - 当我尝试执行$ apply或$ digest时,我收到一个inproc错误。 $ timeout似乎没有改变任何东西。我真的被困在这里了。

HTML

<div class="feedContainerDiv" ng-repeat="question in questions | filter:query | orderBy:orderProp:true">
    <div class="feedName">{{question.name}}</div>
    <div class="feedTitle">{{question.title}}</div>
    <div class="feedQuestion>{{question.question}}</div>
</div>

在控制器

if (updateInterval > 0) {
uiMil = updateInterval * 1000;
$interval(function() {
    QuestionFactory.getNewQuestions()
        .then(function(response) {
            if (response.data[0] !== undefined) {
                var pushObject = new Object();
                for (i = 0; i < response.data.length; i++) {
                    pushObject = response.data[i];
                    $scope.questions.push(pushObject);
                }
            }
        });
}, uiMil);
}

1 个答案:

答案 0 :(得分:0)

问题是我在某些条件下正在重新加载上下文,因此每次我这样做时都会创建一个新的作用域。我试图引用$ scope.questions期望正在重新加载陈旧内容的内容。

始终确保您在预期范围内工作!这是我在这里学到的教训。