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

AngularJS: Using list item as variable for separate list

本文关键字:列表 单独 变量 AngularJS      更新时间:2023-09-26

在我的控制器中,我有这些数组里面有对象项

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

美元范围。totalNames[i]返回正确的数组;然而,一旦我添加。quote到它,它返回未定义。什么好主意吗?由于

$scope.totalNames是list的list。你应该这样做:

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);
    }
}