【发布时间】:2015-08-10 22:43:29
【问题描述】:
我很难理解为什么当我在任何状态下按下刷新按钮时,它总是会重新加载游戏状态。状态之间的唯一区别是游戏状态在成功登录后使用 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 = '';
}
});
}]);
【问题讨论】:
-
点击 reload 前后的 URL 是什么样的?猜测一下,我会说您在
$locationProvider中使用 HTML5 模式,而没有 configuring server-side URL rewriting。 -
你在哪里定义登录逻辑?我没有看到登录页面的登录控制器?
-
我添加了更多代码,希望对您有所帮助。
-
例如,如果我在这里:localhost:3010/#/createGame 并刷新我会回到这里:localhost:3010/#/games。
标签: angularjs angular-ui-router