无法访问javascript数组

时间:2016-05-07 21:44:06

标签: javascript arrays angularjs-scope angularjs-ng-repeat undefined

我在工厂中有一个函数返回一个数组(highestEd是上面定义的数组)。

dataService.listHighestEd = function(){
    return highestEd;
}

当我将返回值存储到$ scope变量中并使用ng-repeat时,它可以正常工作。但是,当我将它存储在$ scope变量中并尝试从中访问一个元素时,它表示它未定义。

$scope.education = dataService.listHighestEd();
if ($scope.education[0] == "College"){
    //Do Stuff
}

我已经使用了console.log($ scope.education),我可以看到数组填充了数据并且长度为102,但是如果我执行console.log($ scope.education.length)它会返回0或console.log($ scope.education [0])返回undefined。

我不知道为什么会发生这种情况,因为当我在UI中的同一个数组上使用ng-repeat时,它会完美地访问信息。

对于这个问题的任何见解都会非常感激,因为这对我即将到来的截止日期非常重要。

下面是正在运行的代码和控制台输出的图像。数组的内容沿底部可见,但array.length的结果为0。

Image of controller being run and output in the console.

0 个答案:

没有答案
相关问题