【问题标题】:AngularJS: Using list item as variable for separate listAngularJS:使用列表项作为单独列表的变量
【发布时间】:2014-07-13 03:27:21
【问题描述】:

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

$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 添加到它,它就会返回未定义。有任何想法吗?谢谢

【问题讨论】:

    标签: javascript arrays angularjs variables scope


    【解决方案1】:

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

    【讨论】:

    • 我的错,没看到那个。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 2020-01-03
    • 1970-01-01
    相关资源
    最近更新 更多