2015-09-27 138 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); 
};