【问题标题】:Not able to loop through Angular array?无法循环通过 Angular 数组?
【发布时间】:2016-07-15 13:13:03
【问题描述】:

我试图循环遍历这个数组。

但是,它并没有向我显示 {{repeat.title}} 中的任何内容

我使用以下 HTML/Angular 代码循环遍历数组:

<div class="column inline inline-4 center choice" ng-repeat="repeat in repeater.keuze_menu[0]">
    <span>{{repeat.title}}</span>
</div>

角度对象

$scope.repeater = [{
    "uitgelichte_afbeelding": false,
    "content_afbeelding": "http:\/\/hetplein.h2505194.stratoserver.net\/wp-content\/uploads\/2016\/05\/image2.jpg",
    "keuze_menu": [{
        "keuze_1": {
            "ID": 81,
            "id": 81,
            "title": "IJs",
            "filename": "Ijs-1.jpg",
            "url": "http:\/\/hetplein.h2505194.stratoserver.net\/wp-content\/uploads\/2016\/05\/Ijs-1.jpg",
            "alt": "",
            "author": "1",
            "description": "",
            "caption": "",
            "name": "ijs-2",
            "date": "2016-07-14 09:32:06",
            "modified": "2016-07-15 11:37:33",
            "mime_type": "image\/jpeg",
            "type": "image",
            "icon": "http:\/\/hetplein.h2505194.stratoserver.net\/wp-includes\/images\/media\/default.png",
            "width": 1440,
            "height": 956,
            "sizes": {
                "thumbnail": "http:\/\/hetplein.h2505194.stratoserver.net\/wp-content\/uploads\/2016\/05\/Ijs-1-150x150.jpg",
                "thumbnail-width": 150,
                "thumbnail-height": 150,
                "medium": "http:\/\/hetplein.h2505194.stratoserver.net\/wp-content\/uploads\/2016\/05\/Ijs-1-300x199.jpg",
                "medium-width": 300,
                "medium-height": 199,
                "medium_large": "http:\/\/hetplein.h2505194.stratoserver.net\/wp-content\/uploads\/2016\/05\/Ijs-1-768x510.jpg",
                "medium_large-width": 768,
                "medium_large-height": 510,
                "large": "http:\/\/hetplein.h2505194.stratoserver.net\/wp-content\/uploads\/2016\/05\/Ijs-1-1024x680.jpg",
                "large-width": 1024,
                "large-height": 680
            }
        }
    }]
}]

我在这里做错了什么?

【问题讨论】:

  • 你想遍历一个对象,而不是一个数组...

标签: javascript angularjs multidimensional-array


【解决方案1】:

您希望遍历对象中恰好位于数组第一个索引中的键。为此,您想使用ng-repeat

<div ng-repeat="(key, value) in repeater.keuze_menu[0]">
    <span>{{value.title}}</span>
</div>

【讨论】:

【解决方案2】:

使用 angular.forEach() https://docs.angularjs.org/api/ng/function/angular.forEach

angular.forEach(values, function(value, key) {
  this.push(key + ': ' + value);
});

【讨论】:

    【解决方案3】:

    在尝试了我能想到的一切之后,我终于找到了解决方案。

    <div class="column inline inline-4 center choice" ng-repeat="repeat in repeater">
        <div ng-repeat="value in repeat.keuze_menu[0]">
            <span>{{value.title}}</span>
            <img alt="" src=""/>
        </div>
    </div>
    

    我只需要呼叫中继器两次。

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 2017-07-02
      相关资源
      最近更新 更多