javascript - Angular : Updating $scope with new data causes old data to remain when using ng-repeat

标签 javascript angularjs arrays angularjs-scope

我遇到了一个奇怪的问题,我用新数据替换了 $rootScope.data.vehicles 中的值,但我 View 中的旧数据点与新数据一起保留了大约一秒钟。

例如,在我用新数组替换 $rootScope.data.vehicles 数组后,我的 View 将首先显示 3 个新值,然后显示 3 个旧值。 View 在 $rootScope.data.vehicles 上使用 ng-repeat 来显示图 block 。

immediately after replacing array

大约一秒后, View 中不再显示 3 个旧值。

one second after replacing array

每次间隔触发时,即使值没有改变,我也会得到新值,然后是旧值。

interval continues to refresh

我的 Controller 和工厂看起来像这样:

(function () {
    var vehiclesInjectParams = ['$location', 'dataService', '$rootScope', 'VehiclesRefreshService'];

    var VehiclesController = function ($location, dataService, $rootScope, VehiclesRefreshService) {
        var vm = this;

        if ($rootScope.data == undefined)
            $rootScope.data = {};

        $rootScope.data.vehicles = [];

        function init() {
            dataService.getVehicles()
                .then(function (data) {
                    $rootScope.data.vehicles = data.results;
                }, function (error) {
                    var thisError = error.data.message;
                });

            VehiclesRefreshService.getValues();
        };

        init();
    };

    VehiclesController.$inject = vehiclesInjectParams;

    var vehiclesRefreshInjectParams = ['$interval', '$rootScope', '$q', 'dataService'];

    var VehiclesRefreshService = function ($interval, $rootScope, $q, dataService) {
        var factory = {};

        factory.getValues = function () {
            var interval = $interval(function () {
                dataService.getVehicles()
                .then(function (data) {
                    $rootScope.data.vehicles = data.results;
                }, function (error) {
                    var thisError = error.data.message;
                });
            }, 10000);

        };

        return factory;
    };

    VehiclesRefreshService.$inject = vehiclesRefreshInjectParams;

    angular.module('teleAiDiagnostics').controller('VehiclesController', VehiclesController).factory('VehiclesRefreshService', VehiclesRefreshService);
}());

首先,我加载数组,然后启动 $interval 计时器,每 10 秒刷新一次值。一旦 dataService 返回新的值列表并将它们放入 $rootScope.data.vehicles 中,我就会注意到有 6 个图 block ,而不是 3 个。一秒钟后,我只剩下 3 个新图 block 。

我的观点是这样的:

<header>
    <h1>Vehicles</h1>
</header>

<article class="icon-gallery flexslider">
    <section>
        <figure ng-repeat="vehicle in $parent.data.vehicles" class="gallery-item svg">
            <a class="nextpage" ng-href="#!/vehicle/{{vehicle.vehicleID}}">
                <img src="img/machine/01.svg" alt="">
                <span class="green-machine-code">{{vehicle.id}}</span>
            </a>
            <ul>
                <li>{{vehicle.id}}</li>
                <li>{{vehicle.ip}}</li>
                <li>Online: {{vehicle.online}}</li>
                <li>Status: {{vehicle.status}}</li>
                <li>AllClear: {{vehicle.allClear}}</li>
            </ul>
        </figure>
    </section>
</article>

关于如何让我的图 block 无缝刷新有什么想法吗?非常感谢所有帮助。

最佳答案

避免track by $index当有一个唯一的属性标识符可以使用时。

<figure ng-repeat="vehicle in $parent.data.vehicles track by ̲v̲e̲h̲i̲c̲l̲e̲.̲i̲d̲ ̶$̶i̶n̶d̶e̶x̶" class="gallery-item svg">
    <a class="nextpage" ng-href="#!/vehicle/{{vehicle.vehicleID}}">
        <img src="img/machine/01.svg" alt="">
        <span class="green-machine-code">{{vehicle.id}}</span>
    </a>
    <ul>
        <li>{{vehicle.id}}</li>
        <li>{{vehicle.ip}}</li>
        <li>Online: {{vehicle.online}}</li>
        <li>Status: {{vehicle.status}}</li>
        <li>AllClear: {{vehicle.allClear}}</li>
    </ul>
</figure>

来自文档:

If you are working with objects that have a unique identifier property, you should track by this identifier instead of the object instance. Should you reload your data later, ngRepeat will not have to rebuild the DOM elements for items it has already rendered, even if the JavaScript objects in the collection have been substituted for new ones. For large collections, this significantly improves rendering performance.

— AngularJS ng-repeat Directive API Reference - Tracking

关于javascript - Angular : Updating $scope with new data causes old data to remain when using ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45442428/

相关文章:

javascript - 访问 Node js中对象内部的数组 - 未定义?

javascript - 在 WebAssembly 中写入文件

javascript - 错误: [$injector:unpr] Unknown provider: $cordovaSmsProvider <- $cordovaSms <- smsController

python - Numpy:如何添加两个从不同类型转换而来的 np?

javascript 类 - 每次使用都需要 "this.varname"吗?

javascript - 如何使 componentDidMount() 在渲染之前完成

angularjs - Angular 2动态生成的表单组

html - 文本未与相应的复选框对齐

c - C中sprintf的段错误

php - 如何检查数组的某个部分是否存在于另一个数组中?