已移除的Firebase对象未在视图

时间:2015-08-08 00:52:48

标签: angularjs angularjs-ng-repeat firebase angularfire

我正在尝试从$ scope.joinedGames变量中删除项目,将其设置为空数组。该变量在视图中用于ng-repeat。使用我的代码,玩家将从Firebase中的玩家对象中的相应游戏中删除,但在我刷新之前视图中没有任何变化。我之前已经完成了这个(joinGames。$ 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>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

$scope.joinedGames = $firebaseArray(joinedGamesRef);