1
我想要做的是獲得遊戲ID爲APIService.postData
創建的遊戲。然後我需要將這個Game ID放到Angular foreach循環中,這樣在RESTful方面,外鍵約束就成立了。外部範圍從角度forEach訪問
如何從那裏獲得該遊戲ID?
P.S.我很清楚範圍問題
this.createGame = function() {
APIService.postData('game', '', $scope.newGameData).then(function (data) {
$scope.newGameID = data.id;
});
// Looping through each added class and adding the game_id onto the object in
// order for the DB insertion to go smoothly on the RESTful side.
angular.forEach($scope.newGameData.classes, function (key, value) {
$scope.newGameData.classes[value].game_id = $scope.newGameID;
APIService.postData('game-class', '', $scope.newGameData.classes[value]);
});
// Looping through each added race and pushing the game_id onto the object in
// order for the DB insertion to go smoothly on the RESTful side.
angular.forEach($scope.newGameData.races, function (key, value) {
$scope.newGameData.races[value].game_id = $scope.newGameID;
APIService.postData('game-race', '', $scope.newGameData.races[value]);
});
$scope.newGameData = {
name: ""
};
$scope.race_counter = 0;
$scope.class_counter = 0;
$scope.newGameData.classes = [{id: $scope.class_counter}];
$scope.newGameData.races = [{id: $scope.race_counter}];
$scope.successMessage = "New 'Game' has been added!";
$scope.action = 'showGames'; // Default action.
this.getGames();
$window.scrollTo(0, 0);
};
您從承諾填充'$ scope.newGameID'一些延遲後使所有的代碼應該是承諾回調 –
對不起裏面,但是這並沒有任何意義了我。您是否需要查看代碼,例如API服務代碼? – user2077115
所以,承諾回調(根據我的理解)是'then'函數中'data'中的數據。所以,當我做了我正在做的與data.id並將其分配給$ scope.newGameID確實有適當的值。但是,我需要將該值降至Angular forEach循環,並且由於範圍中斷,$ scope.newGameID在這些循環中未定義。 – user2077115