无法设置未定义angularjs的属性'学生'

时间:2022-12-02 00:02:31

I set ClassesServices to classes array.

我将ClassesServices设置为classes数组。

And then, I add students of the class to new property, classes.students but It landed as Cannot set property 'students' of undefined.

然后,我将课程的学生添加到新的属性,classes.students但它降落为无法设置未定义的属性'学生'。

$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();

Appreciate to any helps.

感谢任何帮助。

I wish to create separate function to add students but I got underfined. So I am not so sure it can be use separate function as below.

我希望创建单独的功能来添加学生,但我得到了底层。所以我不太确定它可以使用如下的单独功能。

$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();

3 个解决方案

#1


1  

There seem to be a number of issues here.

这里似乎有很多问题。

First, in your first example you are defining a function in a loop - this will not produce the results you expect.

首先,在第一个示例中,您将在循环中定义一个函数 - 这不会产生您期望的结果。

Second, you have some confusion in the second approach - you are passing .students, which at that time is undefined, to $scope.getStudentsbyClassId expecting this function to the property inside the array - that wouldn't work like that.

其次,你在第二种方法中有一些困惑 - 你传递的.students,当时是未定义的,$ scope.getStudentsbyClassId期望这个函数到数组内的属性 - 这不会像那样工作。

I recommend to get the promises of getStudentsbyClassId and execute a $q.all.

我建议获得getStudentsbyClassId的承诺并执行$ q.all。

(And, by the way, unless you intend to invoke a function from the View, there is no need to define it on the $scope)

(顺便说一句,除非你打算从View中调用一个函数,否则不需要在$ scope上定义它)

$scope.classes = [];
ClassesServices.all()
  .then(function(classesData){
     $scope.classes = classesData;

     var promisesForStudents = [];
     for(var i = 0; i < $scope.classes.length; i++){
       var classId = $scope.classes[i].id;
       promisesForStudent[i] = ClassesServices.getStudentsbyClassId(classId);
     }

     return $q.all(promisesForStudents);
  })
  .then(function(studentsData){
     for(var i = 0; i < studentsData.length; i++){
        $scope.classes[i].students = studentsData[i];
     }
  })

#2


0  

You are referencing $scope.classes and $scope.Classes. In angular capitalization matters. If you change your references from $scope.Classes to $scope.classes you should be all set.

您正在引用$ scope.classes和$ scope.Classes。在角度大写问题上。如果将引用从$ scope.Classes更改为$ scope.classes,则应该全部设置。

#3


0  

Try changing Classes to classes in all the places you have "Classes". Objects and variables names are case-sensitive. So you are refering two diferents objects.

尝试在您拥有“类”的所有位置将类更改为类。对象和变量名称区分大小写。所以你要引用两个不同的对象。

#1


1  

There seem to be a number of issues here.

这里似乎有很多问题。

First, in your first example you are defining a function in a loop - this will not produce the results you expect.

首先,在第一个示例中,您将在循环中定义一个函数 - 这不会产生您期望的结果。

Second, you have some confusion in the second approach - you are passing .students, which at that time is undefined, to $scope.getStudentsbyClassId expecting this function to the property inside the array - that wouldn't work like that.

其次,你在第二种方法中有一些困惑 - 你传递的.students,当时是未定义的,$ scope.getStudentsbyClassId期望这个函数到数组内的属性 - 这不会像那样工作。

I recommend to get the promises of getStudentsbyClassId and execute a $q.all.

我建议获得getStudentsbyClassId的承诺并执行$ q.all。

(And, by the way, unless you intend to invoke a function from the View, there is no need to define it on the $scope)

(顺便说一句,除非你打算从View中调用一个函数,否则不需要在$ scope上定义它)

$scope.classes = [];
ClassesServices.all()
  .then(function(classesData){
     $scope.classes = classesData;

     var promisesForStudents = [];
     for(var i = 0; i < $scope.classes.length; i++){
       var classId = $scope.classes[i].id;
       promisesForStudent[i] = ClassesServices.getStudentsbyClassId(classId);
     }

     return $q.all(promisesForStudents);
  })
  .then(function(studentsData){
     for(var i = 0; i < studentsData.length; i++){
        $scope.classes[i].students = studentsData[i];
     }
  })

#2


0  

You are referencing $scope.classes and $scope.Classes. In angular capitalization matters. If you change your references from $scope.Classes to $scope.classes you should be all set.

您正在引用$ scope.classes和$ scope.Classes。在角度大写问题上。如果将引用从$ scope.Classes更改为$ scope.classes,则应该全部设置。

#3


0  

Try changing Classes to classes in all the places you have "Classes". Objects and variables names are case-sensitive. So you are refering two diferents objects.

尝试在您拥有“类”的所有位置将类更改为类。对象和变量名称区分大小写。所以你要引用两个不同的对象。