DOM不断重建与角度选择ng重复和跟踪

DOM keeps being rebuilding with angular-selectize ng-repeat and track by

本文关键字:ng 跟踪 选择 重建 DOM      更新时间:2023-09-26

我正在使用AngularJS和angular-selectize模块。

为了用我需要的元素填充页面,我使用这样的东西:

索引.html

<selectize
    ng-repeat="select in selects_data track by $index"
    config="selectizeConfig"
    options="select.options"
    ng-model="selects_models[select.name]">
</selectize>

控制器.js

$scope.update_data = function(){
    //I'm using AngularJS resource to get the JSON data from the server
    $scope.selects_data = StatesTableControl.get({'path': $scope.element.path},
        function(data){
            //Success
            angular.forEach(data, function(item){
                $scope.selects_models[item.name] = item.current_id
            });
        }, function(error){
            //Error
        }
    );
};
$scope.update_data();
$interval($scope.update_data, 3000);

无论我是否使用 track by $indextrack by select.name 或根本不使用它,每次我用从服务器获得的数据更新 selects_data 数组时,所有<selectize></selectize>元素都会完全重绘,即使更新后的数组内容相同。

我还没有找到任何自己解决它的食谱。我不明白为什么当我在div或其他元素中使用它时track by会帮助做同样的事情。

如果有人可以帮助解决这个问题,我会很高兴!

我认为

这与你跟踪项目的方式没有任何关系。如果StatesTableControl.get每次调用它时都返回新对象,我认为 angular 认为它们是不同的对象,即使它们包含相同的数据。

您可以仅发送已更改的对象,也可以维护对象的版本号,以便知道未更改的对象并且不替换它们。