Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm new to Angular and I'm trying to set up a simple display. I've looked through the documentation on ng-repeat and have done some of the tutorials with success every time. However, when I went to do a mock of my own I can't get anything to show from the JSON file. Even using the often found Angular "todo.json" example found everywhere I still am unable to figure this one out. I'm not sure if it has to do something with JSON or possibly nesting the ng-repeat. Can anyone guide me to seeing the light?! hehe. Thanks in advance.

So, here's the Plunker link. http://plnkr.co/edit/8rNgPHUHEe88Gpw6aM1D

HTML:

    <!doctype html>
<html ng-app="App" >
<head>
  <meta charset="utf-8">
  <title>Todos $http</title>
  <link rel="stylesheet" href="style.css">
  <script>document.write("<base href=\"" + document.location + "\" />");</script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
  <script src="app.js"></script>
</head>
<body ng-controller="Calendar">
  <ul>
    <li ng-repeat="items in events">
      {{items.events}}
    </li>
  </ul>
</body>
</html>

JS:

var App = angular.module('App', []);

App.controller('Calendar', function($scope, $http) {
  $http.get('todos.json')
       .then(function(res){
          $scope.events = res.data;                
        });
});

JSON:

[
    {
        "events": [
            {
                "EventTitle": {
                    "href": "http://example.com/event1",
                    "text": "HEADLINE TEXT FOR EVENT 1"
                },
                "HeadlineImage": {
                    "href": "http://example.com/event1",
                    "src": "http://example.com/Image.jpg",
                    "text": "CAPTION TEXT FOR IMAGE "
                },
                "Eventdescription": "Lorem Loreem Loreeem Ipsum Ipsuum Ipsuuum ..."
            }
        ]
    }
]
share|improve this question
up vote 1 down vote accepted

If you must keep this structure, then you need to do something like this

<ul>
  <li ng-repeat="items in events">
    <a href="{{items.EventTitle.href}}">{{items.EventTitle.text}}</></a>
  </li>
</ul>

controller:

App.controller('Calendar', function($scope, $http) {
  $http.get('todos.json')
       .then(function(res){
          $scope.events = res.data[0].events;             
        });
});

Here's a forked plunker

Edit: The revised plunker, using the above changes. The scope var should be res.data.events

Updating after a new json structure provided: Here's a working example with actual json data

share|improve this answer
    
Ahhhh.. I see!!! Thank you! – Cedric Nov 24 '14 at 23:16
    
I edited this, the ng-repeat will work with some modification so chack the revised code. Cheers. – alou Nov 25 '14 at 5:51
    
Thanks this has worked. Now that I have the JSON working locally, I'm wondering, and forgive me for being so new at this, but how would I interpret this to link to the EXTERNAL JSON file? I've tried to just put the link in the $Http GET line, but it's not working is there a simple solution to link what is currently the LOCAL json to an EXTERNAL json? I've been scouring StackOverflow for something like this, but all I'm getting is $ajax. Is that the correct way to link an external JSON file in Angular? – Cedric Nov 26 '14 at 19:17
    
It should work if the get requist is producing exactly this json format, what's the url you are using? Also, have in mind, this functionality should be in a service and then inject the service to the controller. – alou Nov 26 '14 at 20:46
    
I'm trying to get my head around this... Here's the plunker. I have also pasted the contents of that JSON file into the todo.json file there. – Cedric Nov 26 '14 at 21:40

Your data structure is pretty weird. Check an updated working plunker: http://plnkr.co/edit/SXHjqxjZ2bgJSs327jz4?p=preview

You can use <pre>{{ events | json }}</pre> in your view to easily inspect/debug objects.

share|improve this answer
    
Hmm.. Is there anyway to work within this particular format since the API is structuring the JSON like this directly from kimono.io? – Cedric Nov 24 '14 at 22:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.