【问题标题】:$scope variable value flicks in setInterval$scope 变量值在 setInterval 中闪烁
【发布时间】:2017-06-22 16:32:17
【问题描述】:

以下是我用来更新列表计数的代码, 起初它在计数时工作正常,但随着计数改变值轻弹。 例如:如果 count 一开始是 3,然后 count 更新为 4。count 变量在 3 和 4 之间闪烁。

setInterval(function () {
    if ($scope.listing != undefined && $scope.listing != null) {
        $rootScope.countsData = $scope.listing.length;
    } else {
        $rootScope.countsData = 0;
    }
}, 2000);

【问题讨论】:

  • 格式化/缩进你的代码,它是不可读的。
  • 那么当count改成4的时候,你还想看3吗?为什么?
  • 我不想看到 3,但它同时显示了 3 和 4。我只想看 4 个。

标签: javascript angularjs angularjs-scope setinterval flicker


【解决方案1】:

将 $interval 注入您的控制器,然后将您的代码更改为:

$interval(function() {
    if ($scope.listing) {
        $rootScope.countsData = $scope.listing.length;
    } else {
        $rootScope.countsData = 0;
    }
}, 2000);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多