After setting ClassesServices to the classes array, I tried adding students of the class to a new property called classes.students. However, I encountered an error message saying 'Cannot set property 'students' of undefined.'
$scope.classes = [];
$scope.getClasses = function(){
ClassesServices.all().then(function(data){
$scope.classes = data;
for(var i = 0; i < $scope.classes.length; i++){
ClassesServices.getStudentsbyClassId($scope.classes[i].id).then(function(data){
$scope.classes[i].students = data;
});
}
});
};
$scope.getClasses();
Your help is much appreciated in resolving this issue.
I attempted to create a separate function to add students but ended up with an 'undefined' outcome. Hence, I'm unsure if using a separate function as shown below would be feasible.
$scope.getClasses = function(){
ClassesServices.all().then(function(data){
$scope.classes = data;
for(var i = 0; i < $scope.classes.length; i++){
$scope.getStudentsbyClassId($scope.classes[i].students, $scope.classes[i].id);
});
}
});
};
$scope.getStudentsbyClassId = function (students){
ClassesServices.getStudentsbyClassId(id).then(function(data){
students = data;
};
$scope.getClasses();