ui-router状态不刷新

时间:2015-08-10 22:43:29

标签: angularjs angular-ui-router

我很难理解为什么当我在任何状态下按下刷新按钮时,它总会重新加载游戏状态。状态之间的唯一区别是游戏状态在成功登录后加载了state.go(' games')。当按下刷新按钮时,我必须添加/更改状态才能重新加载状态。

game.config(['$stateProvider', function ($stateProvider) {
  $stateProvider.state('signUp', {
   url: '/signUp',
   templateUrl: 'templates/signUp.html'
  });

  $stateProvider.state('login', {
   url: '/login',
   templateUrl: 'templates/login.html'
  });

  $stateProvider.state('games', {
   url: '/games',
   templateUrl: 'templates/games.html'
  });

  $stateProvider.state('dashboard', {
   url: '/dashboard',
   templateUrl: 'templates/dashboard.html',
   controller: 'dashboard.controller'
  });

  $stateProvider.state('createGame', {
   url: '/createGame',
   templateUrl: 'templates/createGame.html'
  });

  // $stateProvider.state('viewGame', {
  //  url: '/viewGame',
  //  templateUrl: 'templates/viewGame.html',
  //  controller: 'viewGame.controller'
  // });
}]);

games.controller('games.controller', ['$scope', '$state', '$stateParams', 'Auth', '$firebaseArray','Fire', function ($scope, $state, $stateParams, auth, $firebaseArray, fire) {

  $scope.games = $firebaseArray(fire.child('games'));
  // $scope.players = $firebaseArray(fire.child('players'));
  $scope.view = 'listView';

  $scope.setCurrentGame = function(game) {
    $scope.currentGame = game;
  };

  $scope.createGame = function() {
    if ($scope.format == 'Match Play') {
      $scope.skinAmount = 'DOES NOT APPLY';
      $scope.birdieAmount = 'DOES NOT APPLY';
    }
    $scope.games.$add({
      name: $scope.gameName,
      host: $scope.user.name,
      date: $scope.gameDate,
      location: {
        course: $scope.courseName,
        address: $scope.courseAddress
      },
      rules: {
        amount: $scope.gameAmount,
        perSkin: $scope.skinAmount,
        perBirdie: $scope.birdieAmount,
        format: $scope.format,
        holes : $scope.holes,
        time: $scope.time
      }
    })
    $state.go('games');
  };

  $scope.check = function(game) {
    console.log(game.players)
  };

  $scope.addPlayer = function(game) {
    console.log(game.$id);
    var ref = fire.child('players').child(game.$id);
    ref.push({
      id : $scope.user.id,
      name : $scope.user.name,
      email : $scope.user.email
    })
  };

  // swap DOM structure in games state
  $scope.changeView = function(view){
    $scope.view = view;
  }

}]);

games.controller('app.controller', ['$scope', '$state', '$stateParams', 'Auth', 'Fire', function ($scope, $state, $stateParams, auth, fire) {
  $scope.user = {
    email : '',
    password : ''
  };

  $scope.signUp = function() {
    auth.$createUser($scope.user)
    .then(function(userData) {
      // After successful signup save a user record to users under their auth ID
      fire.child('users').child(userData.uid).set({
        name : $scope.user.name,
        email : $scope.user.email,
        joined : Date.now()
      });
      $state.go('games');
      console.log("User " + userData.uid + " created successfully!");
    })
    .catch(function(error) {
      console.error("Error: ", error);
    });
  };
  $scope.login = function() {
    auth.$authWithPassword($scope.user).catch(function(error) {
      console.error("Authentication failed:", error);
    });
  };
  $scope.logout = function() {
    auth.$unauth();
    window.location = '/';
  };

  auth.$onAuth(function(authData) {
    if (authData) {
      $scope.activeUser = authData;

      // After user logs in find user record by auth id
      fire.child('users').child(authData.uid).on('value', function(snapshot) {
        // Checks if user exsists
        if (snapshot.exists()) {
          // sets scope user to the user data
          $scope.user = snapshot.val();
          // sets scope user id to the auth id
          $scope.user.id = snapshot.key();
        }
      });
      console.log("Logged in as:", authData.uid);
      $state.go('games');
    } else {
      $scope.activeUser = false;
      // $scope.user = '';
    }
  });
}]);

1 个答案:

答案 0 :(得分:-1)

只需为游戏状态创建一个控制器。

请确保将$ route服务包含到您的范围中并执行以下操作:

$route.reload();