简体   繁体   中英

Using ng-repeat on a JSON object doesn't work - why?

I can't seem to iterate over a JSON object using ng-repeat. I did test it directly by index and it works, but just can't seem to loop and print. What am I doing wrong? JSFiddle link .

Here's my code:

<div ng-app="myApp">

<div ng-controller="Ctrl">

    <h3>Upcoming Events</h3>

    <ul ng-repeat="event in events">
        <li>{{ event.feed.entry.title.$t }}</li>    
    </ul>

    <!-- testing single entry here - it works! -->
    <small>{{ events.feed.entry[0].title.$t }}</small>

</div>

</div>

The script:

var app = angular.module('myApp', []);
var feedUrl = 'https://www.google.com/calendar/feeds/ogmda.com_89pas0l9jbhpf053atd83hdj30%40group.calendar.google.com/public/basic?alt=json';

app.controller('Ctrl', function($http, $scope) {
$http.get(feedUrl).success(function(data) {
    $scope.events = data;
    });
});

That's because you iterate over the whole data returned by the calendar query, and not over the entries themselves. Change your ul to:

<ul ng-repeat="entry in events.feed.entry">
    <li>{{ entry.title.$t }}</li>    
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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