0

I am really having a hard time repeating nested objects that are loaded from a json file. I have seen examples of people using dot notations in their HTML for retrieving nested data in JSON, but I can't figure out how this would work for me. The JSON is valid, but I am new to Angular. Could somebody give me a push in the right direction? I would like to enter the name of my menu-card and display it in separate lists. This is what I have and it does not work, (the console does not give me any errors if you are wondering):

<div ng-controller="menu" ng-repeat="item in menu.voorgerecht">
        <div>{{item.naam}}</div>

</div>

js

angular.module("app", [])

.controller("menu", function ($scope, $http) {
            $scope.menu = null;
            $http({
                method: 'GET',
                url: 'menu-items.json'
            }).succes(function (data, status, headers, config) {
            $scope.menu = data;
        }).error(function (data, status, headers, config) {});
});

json

{
        "voorgerecht": [
            {
                "naam": "Sardine"
            },
            {
                "naam": "Funghi Trifolati"
            }
        ],
        "pizza": [
            {
                "naam": "San Marco"
            },
            {
                "naam": "Capriciosa"
            }
        ],
        "desert": [
            {
                "naam": "Sorbet"
            },
            {
                "naam": "Dame Blanche"
            }
        ]
}

2 Answers 2

3

Don't do ng-repeat on the same element that has ng-controller:

<div ng-controller="menu">
  <div ng-repeat="item in menu.voorgerecht">
    <div>{{item.naam}}</div>
  </div>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer, but this does not work. I feel like I am missing something with the structure of my JSON file. It is really becoming a headache :)
What do you mean by "doesn't work"? Does it not display "Sardine" and "Funghi Trifolati"?
Well, you also have an typo - it should be .success with two s. Otherwise, something else is going on that you haven't posted, since this works for me: plnkr.co/edit/Q4EyzU2zf7qhmB8JtOIU?p=preview
You get the credits for spotting the typo. Thanks a lot mate! I always post all my relevant code by the way.
1

This should work

<div ng-controller="menu">
   <div ng-repeat="item in menu.voorgerecht">
      {{ item.naam }}
   </div>
</div>

Here is the fiddle

3 Comments

How is this fundamentally different than my answer posted 15 minutes before?
I have tested this already in a fiddle with a variable. The problem is that when I am loading data from a JSON file I can't get it to work.
typo hah. nice catch @NewDev, BTW, by the time I composed my answer you posted your code. I am new to stack over flow :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.