移除的 Firebase 对象不会在视图中更新



我试图从$范围中删除项目。joinedGames变量被设置为一个空数组。该变量在视图中用于ng-repeat。使用代码,我有玩家从Firebase中的玩家对象内的相应游戏中删除,但在视图中没有更改,直到我刷新。我以前这样做过(joinedGames.$remove(game))直接在ng-repeat中,它的工作如预期的那样。这是我第一次为重复推入一个作用域变量,我想这就是为什么我有问题,但我不能弄清楚。很多谢谢!

game.controller('dashboard.controller', ['$scope', '$state', '$stateParams', 'Auth', '$firebaseArray', '$firebaseObject', 'Fire', function ($scope, $state, $stateParams, auth, $firebaseArray, $firebaseObject, fire) {
  $scope.remove;
  // Get Current signed in user auth data
  var player = auth.$getAuth();
  // Find a user in the database that is equal to the id of the current singed in user
  $scope.player = $firebaseObject(fire.child('users').child(player.uid));
  // set up some empty arrays
  var games = [];
  $scope.joinedGames = [];
  // Anything with $ look at AngularFire docs
  // .child, .on, .key, .val, .forEach all Firebase Web API
  // get a pointer to the list of games that have players
  var playersRef = fire.child('players');
  // set up a callback on the list of games that have players
  playersRef.on('value', function(snapshot) {
    // console.log(snapshot.val());
    // set games equal to all the children
    var games = snapshot;
    // loop through all the children
    games.forEach(function(gameSnapshot) {
      // console.log(gameSnapshot.key())
      // set gameID to the key of the current child
      var gameID = gameSnapshot.key();
      // set players to the children of this game
      var players = gameSnapshot;
      // loop through all the children
      players.forEach(function(playerSnapshot) {
        // console.log(playerSnapshot.val());
        // set player to the data of this child
        var player = playerSnapshot.val();
        // check if this child has a key called id with a value equal to the signed in player's id
        if (player.id === $scope.player.$id) {
          // create a firebase object from the reference to the gameID
          var joinedGame = $firebaseObject(fire.child('games').child(gameID));
          // push the game object into the joinedGames scope variable
          $scope.joinedGames.push(joinedGame);
        }
      });
    });
  });
  $scope.removeDashboardGame = function(game) {
    var gameID = game.$id;
    var thisGame = fire.child('players').child(gameID);
    thisGame.on('value', function(snapshot) {
      snapshot.forEach(function(unqKey){
        var thisPlayer = unqKey.key()
          thisGame.child(thisPlayer).remove();
      })
    })
  }
}]);
<标题> HTML
<div ng-controller="dashboard.controller">
  <h1>Your Games</h1>
  <ul>
    <li class="activeGames" ng-repeat="game in joinedGames">
      <a href="#" ng-click="gameDetails(game)">Course: {{ game.location.course }} <br> Holes: {{ game.rules.holes }}</a>
      <a href="#" ng-click="removeDashboardGame(game)">Remove</a>
    </li>
  </ul>
</div>

我猜你需要在删除该对象后调用$scope.$apply(),因为代码正在回调中运行。

而不是初始化您必须自己管理的空数组(第12-13行)。用AngularFire库创建同步数组。

$scope.joinedGames = $firebaseArray(joinedGamesRef);

相关内容

  • 没有找到相关文章

最新更新