Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS范围仅在某些时候刷新_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS范围仅在某些时候刷新

Javascript AngularJS范围仅在某些时候刷新,javascript,angularjs,Javascript,Angularjs,我有一个从API中提取的卡片列表,并使用ng repeat显示在表中。当我单击删除一条记录时,会弹出一个确认框,如果为true,则删除该记录并刷新整个表。API的响应与我预期的一样,具有正确数量的记录。但有时记录仍然存在。有时候不是 这是控制器: angular.module("frontend").controller("AdminCardsCtrl", function($scope, auth, Card) { var getCards; auth.adminOnly(); g

我有一个从API中提取的卡片列表,并使用ng repeat显示在表中。当我单击删除一条记录时,会弹出一个确认框,如果为true,则删除该记录并刷新整个表。API的响应与我预期的一样,具有正确数量的记录。但有时记录仍然存在。有时候不是

这是控制器:

angular.module("frontend").controller("AdminCardsCtrl", function($scope, auth, Card) {
  var getCards;
  auth.adminOnly();
  getCards = function() {
    Card.query({}, function(cards) {
      $scope.cards = cards;
    });
  };
  getCards();
  $scope.destroy = function(card) {
    var confirmation;
    confirmation = confirm('Are you sure?');
    if (confirmation) {
      Card.get({
        id: card.id_string
      }, function(card) {
        card.$delete();
        getCards();
      });
    }
  };
})
模型:

angular.module('frontend').factory('Card', function($resource, API_URL) {
  return $resource(API_URL + "/cards/:id.json", {
    id: '@id_string'
  }, {
    update: {
      method: 'PUT'
    }
  });
});
下面是视图中依赖于
$scope.cards
的部分

  <tr ng-repeat="card in cards">
    <td><a ng-href="/admin/cards/{{card.id_string}}">{{ card.name }}</a></td>
    <td>{{ card.category }}</td>
    <td><a ng-href="/admin/cards/{{card.id_string}}/edit">Edit</a> / <a ng-click="destroy(card)">Delete</a></td>
  </tr>

{{card.category}
/删除

我可以尝试将单个记录从表中拼接出来,但这也需要更新表条带化,这就是为什么我想刷新整个过程。如果我从
getCards
功能执行
console.log
,则在我加载页面并确认删除后,它将运行。每次删除某些内容时重置
$scope.cards
是否会导致所有表行刷新?为什么会出现不一致的情况?

只有在$delete请求完成后,您才能获得新卡:

card.$delete(getCards);

对资源调用$delete只发送HTTP请求。如果要从视图中删除该项,必须自己执行

您可以尝试以下方法:

card.$delete(..., function() {
    $scope.card.splice(index, 1);
  });

就是这样,
$delete()
是异步的!但是我没能让它按照你建议的语法工作。我做了
card.$delete(函数(){getCards()})
。抱歉,忘记了$resource(编辑了我的答案)的语法。Alexey使用的解释指出了我的错误,但这是正确的语法。