AngularJS:使用列表项作为单独列表的变量

时间:2014-05-23 17:04:59

标签: javascript arrays angularjs variables scope

所以在我的控制器中,我有这些带有对象项的数组:

$scope.johnny = [
    {quote: "Anything for My Princess", path: 'sounds/AnythingForMyPrincess.mp3'},
    {quote: "Don't Even Ask", path: 'sounds/DontEventAsk.mp3'},
    {quote: "Don't Plan Too Much", path: 'sounds/DontPlanTooMuch.mp3'}
] //there are many more lists like this one

我想通过使用另一个数组(数组名称)和for循环来浏览这些列表:

$scope.totalNames = [$scope.johnny, $scope.lisa, $scope.mark, $scope.denny, $scope.lisasmom, 
                    $scope.chicken, $scope.chris, $scope.flower, $scope.mike, $scope.steven]

for (var i = 0; i < $scope.totalNames.length; i++) {
    var list = $scope.totalNames[i];
    alert(list.quote);
}

$ scope.totalNames [i]返回正确的数组;但是,一旦我将.quote添加到它,它返回undefined。有任何想法吗?感谢

1 个答案:

答案 0 :(得分:2)

$scope.totalNames是列表清单。你应该这样:

for (var i = 0; i < $scope.totalNames.length; i++) {
    var list = $scope.totalNames[i];
    for (var j = 0; j < list.length; j++) {
        alert(list[j].quote);
    }
}