Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法读取属性';长度';Angularjs 1.6.6中未定义的_Javascript_Angularjs_Angularjs Directive_Angularjs Scope - Fatal编程技术网

Javascript 无法读取属性';长度';Angularjs 1.6.6中未定义的

Javascript 无法读取属性';长度';Angularjs 1.6.6中未定义的,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,我必须下拉列表,并希望根据第一个列表中的选定值填充第二个列表。我在填充第一个列表时遇到了一个问题。当然,我使用getCurrentSessionCourses()方法从服务器获取数据,并从该数据填充第一个列表。第一个列表在for循环中填充。如果在服务器响应之前执行for循环,则i

我必须下拉列表,并希望根据第一个列表中的选定值填充第二个列表。我在填充第一个列表时遇到了一个问题。当然,我使用getCurrentSessionCourses()方法从服务器获取数据,并从该数据填充第一个列表。第一个列表在for循环中填充。如果在服务器响应之前执行for循环,则i<$scope.Courses.length具有未定义的值。有时数据出现在for循环执行之前,有时数据出现在for循环执行之后。下面是我的AddStudentController.js和相关的HTML


(功能(){
var myApp=angular.module(“myApp”);
var AddStudentController=函数($scope、StudentService、CourseService){
var onCourses=函数(数据){
var结果=[];
对于(变量i=0;i<$scope.Courses.length;i++){
对于(var j=0;j

选课
选课

尝试在
CourseService.getCurrentSessionCourses()之后调用
CourseService.courses()
,方法如下:

CourseService.getCurrentSessionCourses().then(function (courses) { 
    $scope.Courses = courses;
    CourseService.courses().then(onCourses);
});

通过这种方式,当您尝试对
$scope.Courses
进行迭代时,您将确保它具有一定的价值。

尝试初始化
$scope.Courses=[]

angular在呈现页面时,将不会找到任何
$scope.Courses
对象,直到您将响应分配给
$scope.Courses
,从而引发

$scope.Courses.length具有未定义的值


发布您的服务
var myApp = angular.module("myApp");
    $scope.Courses = []; // Add this line
    var AddStudentController = function ($scope,StudentService,CourseService) {

     var onCourses = function (data) {