2015-09-27 248 views
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); 
}; 
+0

您從承諾填充'$ scope.newGameID'一些延遲後使所有的代碼應該是承諾回調 –

+0

對不起裏面,但是這並沒有任何意義了我。您是否需要查看代碼,例如API服務代碼? – user2077115

+0

所以,承諾回調(根據我的理解)是'then'函數中'data'中的數據。所以,當我做了我正在做的與data.id並將其分配給$ scope.newGameID確實有適當的值。但是,我需要將該值降至Angular forEach循環,並且由於範圍中斷,$ scope.newGameID在這些循環中未定義。 – user2077115

回答

0

找出來了。 foreach循環需要進入'then'回調。 GameID一直未定,因爲POST請求還沒有真正完成。最重要的是,$ scope.newGameData被搞砸了,所以我將兩個數組都分配給了它們自己的局部變量,並且效果很好。

this.createGame = function() { 

    var newGameID = ''; 
    var classData = $scope.newGameData.classes; 
    var raceData = $scope.newGameData.races; 

    APIService.postData('game', '', $scope.newGameData).then(function (data) { 
     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(classData, function (key, value) { 
      classData[value].game_id = newGameID; 
      APIService.postData('game-class', '', classData[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) { 
      raceData[value].game_id = newGameID; 
      APIService.postData('game-race', '', raceData[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); 
};