gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:51:29 24 4
gpt4 key购买 nike

我遇到了一个奇怪的问题,我用新数据替换了 $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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com