Javascript 从$rootScope数组中删除ngRepeat项不需要';不要立即从html中删除该项

Javascript 从$rootScope数组中删除ngRepeat项不需要';不要立即从html中删除该项,javascript,angularjs,Javascript,Angularjs,尝试使用angularJS从ul中删除li,成功地从数组中删除元素,但angularJS不会删除li,直到它悬停在上方/对该特定li采取某些操作。代码如下: app.js myApp.run(function($rootScope, appAPIservice){ appAPIservice.getInterests().success(function (response) { $rootScope.interests = []; if (response.data) {

尝试使用angularJS从ul中删除li,成功地从数组中删除元素,但angularJS不会删除li,直到它悬停在上方/对该特定li采取某些操作。代码如下:

app.js

myApp.run(function($rootScope, appAPIservice){ 
  appAPIservice.getInterests().success(function (response) {
    $rootScope.interests = [];
    if (response.data) {
      var interests = response.data;
      for (var i = 0; i < interests.length; i++) {
        $rootScope.interests.push(interests[i]));
      }
    }
  });
});
这将在页面加载时生成以下输出:

<ul class="ng-scope" ng-controller="interestsController">   
  <li class="ng-scope" ng-repeat="interest in interests">
    <a href="#/other-link class="ng-binding">Other Parent/Other Child</a>
    <button ng-click="deleteInterest($index)"><i class="icon-close"></i></button>
  </li>
</ul>
但我收到一条错误消息,说$scope.$digest已在进行中


有没有办法强迫angularJS删除所有ng休假项目?

有几个想法。请尝试使用而不是
$scope.$apply

$timeout(function(){ 
  $scope.interests.splice(arrayIndex, 1);
});
$timeout将在必要时内部触发摘要,但如果摘要已在进行中,则不会抛出该错误

或者,使用CSS隐藏li,直到angular开始删除它,怎么样

.ng-leave { display:none; }

我对ng repeat也有类似的问题。 将项数组长度设置为0后,我必须以长延迟运行超时:

$timeout(function(){ 
  do_some_action
}, 2000);
即使evalAsync也不起作用:

$scope.$evalAsync(function () {
   do_some_action
});
另一个解决办法就是不要考虑旧项目:

var elems = angular.element(document.getElementsByClassName('repeated_items'));
_.each(elems, function (itm) {
   if (itm.$$hashKey) return true;
   do_some_action
});

只是好奇,为什么要使用rootscope呢?根据前面的评论,开始将api逻辑引入InterestController控制器。当您需要动画时,您看到的那些css类非常有用。你的css实现了一些css动画吗?如果是,您需要担心那些css类,否则就不要担心它们。$scope.$apply在这里没有意义,这里的每一段代码都在角度摘要周期内。这就是为什么你会犯这样的错误。停止使用$rootScope并将其放入控制器/$scope中。您不需要在$rootScope上放置任何内容,也不需要使用$scope。$apply。@DanPantry我们使用$rootScope是因为我们在侧栏中显示兴趣,同时允许用户单击页面上的“标志”,将该兴趣添加到侧栏中的兴趣数组中。由于这些都包含在html的不同部分中,有没有一种方法可以在不同的位置调用控制器,即两个不同的div结构?到目前为止,兴趣按钮标志使用了一个指令,所以我不知道如何在其中调用InterestController…“有没有办法在不同的地方调用控制器”-一个服务?css更改是一个很好的解决方法,可以帮助我们构建事物,谢谢。我仍然想弄明白为什么angular在应用之后不自己移除它。我尝试了超时功能,但没有效果。
.ng-leave { display:none; }
$timeout(function(){ 
  do_some_action
}, 2000);
$scope.$evalAsync(function () {
   do_some_action
});
var elems = angular.element(document.getElementsByClassName('repeated_items'));
_.each(elems, function (itm) {
   if (itm.$$hashKey) return true;
   do_some_action
});