删除 Angular 中的选定列表后,列表项未更新

List items is not updating after deleting selected list in Angular

我刚开始使用 Angular,实现起来非常好。现在我正在尝试删除动态创建的列表项。我可以删除项目,但列表项目没有更新。我做了 Google 但没有得到任何解决方案。

这是我的 HTML 东西

<li data-ng-repeat="category in user track by $index">
<div class="pull-left forDrop"><label class="customLabel" id={{category.id}}><a href="" class="viewBooks" ng-mousedown="viewAll(category.id)">{{category.category_name}}</a></label></div>
              <div class="pull-right actions">
                <a href="" class="trash secondry" data-toggle="modal" data-target="#myModalOne" data-ng-click="getCatId($index,category.id)"></a>
                <a href="" class="pvtLock" ng-class="{'lock secondry':category.category_type===1,'openLock secondry':category.category_type===0}"></a>
                <a href="" class="done secondry" data-ng-mousedown="updateCategory()"></a>
                <a href="#" class="penEdit primery"></a>
              </div>

这是controller method

$scope.getCatId = function(index, category_id) {
    cat_id = category_id;
    cat_index = index;
     $scope.user.splice(cat_index,1);
  };

可能是它的错误实现,我不知道。

任何建议都会对我有帮助。

谢谢。

已更新

如果我从底部删除项目,它会更新列表

编辑:等等,你为什么调用 $scope.user.push($scope.user.splice)?只是拼接它。将该行更改为:

$scope.user.splice(cat_index,1)

也尝试将此代码包装在 $timeout 中(尽管您不需要这样做):

$timeout(function() {
    $scope.user.splice(cat_index, 1);
});

并且还在你注入 $scope 的地方注入 $timeout。

您需要致电 $scope.$apply(); 才能使更改生效

$scope.getCatId = function(index, category_id) {
    cat_id = category_id;
    cat_index = index;
    $scope.user.push($scope.user.splice(cat_index,1));
    $scope.$apply()
  };

https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$申请

试试这个代码

 var res= $scope.user;
            // var index =cat_index;
              res.splice(cat_index,1);
              //$scope.user.hideColony= true;
              $scope.user= res;