简体   繁体   English

角度ng-repeat显示对象项目

[英]angular ng-repeat displaying object items

I am having a problem with displaying a JSON array objects that I have gotten from a async http request. 我在显示从异步http请求获取的JSON数组对象时遇到问题。

I have used ng-repeat to try and display the objects in the view but I am having no luck after many hours. 我已经使用ng-repeat尝试在视图中显示对象但是我在几个小时后没有运气。 Here is the code I have so far. 这是我到目前为止的代码。

index.html 的index.html

<div class="main" ng-controller = "MyController">
<ul>
<li ng-repeat="item in parks">
  <div class="info">
    <h2>{{item.parks.name}}<h2>
    <h2>{{item.parks.park_size}}<h2>
    <h2>{{item.parks.open_times}}<h2>
    <h2>{{item.parks.days_closed}}<h2>
    <img ng-src="images/{{item.parks.short}}.jpg">
  </div>
</li>

controllers.js controllers.js

var myApp = angular.module('myApp', []);
    myApp.controller('MyController', ['$scope', '$http', function($scope, $http) {
        $http.get('js/data.json').success(function(data) {
      $scope.parks = data;
      console.log(data);
    });
}]);

data.json data.json

{
    "parks": [
        {
      "park_name": "Central Park",
      "park_size": {
                "miles": "1.2",
                "meters": "1900"
            },
      "open_times": {
                "Monday-Friday": "8am-10pm",
                "Saturday-Sunday": "10am-6.30pm"
            },
            "days_closed": [
                "December 25th",
                "December 26th"
            ],

      "images": [
                {
                    "short": "centralimage1.jpeg"
                },
                {
                    "short": "centralimage2.jpeg"
                },
                {
                    "short": "centralimage3.jpeg"
                },
                {
                    "short": "centralimage4.jpeg"
                }
            ]
        },
    {
      "park_name": "Riverside Park",
      "park_size": {
                "miles": "0.2",
                "meters": "320"
            },
      "open_times": {
                "Monday-Friday": "7am-9pm",
                "Saturday-Sunday": "9am-8.30pm"
            },
            "days_closed": [
                "December 25th",
                "December 26th",
        "Jamuary 1st"
            ],

      "images": [
                {
                    "short": "riversideimage1.jpeg"
                },
                {
                    "short": "riversideimage2.jpeg"
                },
                {
                    "short": "riversideimage3.jpeg"
                },
                {
                    "short": "riversideimage4.jpeg"
                }
            ]
        }





    ]
}

JS fiddle https://jsfiddle.net/owxwh0kz/ JS小提琴https://jsfiddle.net/owxwh0kz/

You are iterating over parks already, so your code can look like this 您已经在公园上进行迭代,因此您的代码可能如下所示

<li ng-repeat="item in parks">
  <div class="info">
    <h2>{{item.park_name}}<h2>

(I changed item.park.name to be item.park_name to match your data) (我将item.park.name更改为item.park_name以匹配您的数据)

Otherwise check using the Angular inspector to see what the data looks like. 否则,请使用Angular检查器检查数据的外观。

It looks like the data coming back from the json object is not consitent with your binding selectors. 看起来从json对象返回的数据与绑定选择器不一致。 Inside an ng-repeat you are alreading interating over the scope of parks . ng-repeat内部,你正在考虑对parks的范围进行整理。

<li ng-repeat="item in parks">
  <div class="info">
    <h2>{{item.park_name}}<h2>
    <h2>{{item.park_size}}<h2> 
    <h2>{{item.open_times}}<h2>
    <h2>{{item.days_closed}}<h2>

For the images because they are in another array you would have to performn another repeat to grab all images.Note that nested ng-repeats can lead to negative performance impacts. 对于图像,因为它们在另一个阵列中,您将不得不执行另一次重复以获取所有图像。请注意,嵌套的ng-repeats可能会导致负面的性能影响。

Based on your code, you should iterate over parks.parks to reach the array of the $scope.parks object. 根据您的代码,您应该遍历parks.parks以到达$scope.parks对象的数组。

Also, within the ng-repeat you should use item.* instead of item.parks.* 此外,在ng-repeat你应该使用item.*而不是item.parks.*

Here is JSFiddle with the fixed code: https://jsfiddle.net/x0ja420L/ 这是JSFiddle的固定代码: https ://jsfiddle.net/x0ja420L/

Some code changes has been made in your code : 您的代码中已进行了一些代码更改:

  • Proper closing of the HTML tags to work it properly. 正确关闭HTML标记以正常工作。

Use <h2>{{item.park_name}}</h2> instead of <h2>{{item.park_name}}<h2> 使用<h2>{{item.park_name}}</h2>代替<h2>{{item.park_name}}<h2>

  • ng-repeat directive iterate the array object so if you want to access park_name you have to use ng-repeat like this ng-repeat="item in parks.parks" ng-repeat指令迭代数组对象,所以如果你想访问park_name你必须使用像ng-repeat那样的ng-repeat="item in parks.parks"

demo ! 演示!

ng-repeat directive iterate the array object, so if you want to access park_name you have to use ng-repeat like this ng-repeat="item in parks.parks" ,code should like this ng-repeat指令遍历数组对象,因此,如果您要访问park_name你必须使用ng-repeat这样的ng-repeat="item in parks.parks" ,代码应该喜欢这个

  <li ng-repeat="item in parks.parks">
  <div class="info">
    <h2>{{item.park_name}}<h2>
    <h2>{{item.park_size}}<h2>
    <h2>{{item.open_times}}<h2>
    <h2>{{item.days_closed}}<h2>
    <img ng-src="images/{{item.images[0].short}}.jpg">
  </div>
</li>

you can see the demo 你可以看到演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM