javascript - Cannot set property 'students' of undefined angularjs -


i set classesservices classes array.

and then, add students of class new property, classes.students landed cannot set property 'students' of undefined.

$scope.classes = []; $scope.getclasses = function(){             classesservices.all().then(function(data){                 $scope.classes = data;                 for(var = 0; < $scope.classes.length; i++){                     classesservices.getstudentsbyclassid($scope.classes[i].id).then(function(data){                         $scope.classes[i].students = data;                     });                 }              });         };          $scope.getclasses(); 

appreciate helps.

i wish create separate function add students got underfined. not sure can use separate function below.

$scope.getclasses = function(){             classesservices.all().then(function(data){                 $scope.classes = data;                 for(var = 0; < $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(); 

there seem number of issues here.

first, in first example defining function in loop - this not produce results expect.

second, have confusion in second approach - passing .students, @ time undefined, $scope.getstudentsbyclassid expecting function property inside array - wouldn't work that.

i recommend promises of getstudentsbyclassid , execute $q.all.

(and, way, unless intend invoke function view, there no need define on $scope)

$scope.classes = []; classesservices.all()   .then(function(classesdata){      $scope.classes = classesdata;       var promisesforstudents = [];      for(var = 0; < $scope.classes.length; i++){        var classid = $scope.classes[i].id;        promisesforstudent[i] = classesservices.getstudentsbyclassid(classid);      }       return $q.all(promisesforstudents);   })   .then(function(studentsdata){      for(var = 0; < studentsdata.length; i++){         $scope.classes[i].students = studentsdata[i];      }   }) 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -