0

Here is my simple code:

in app.js:

$http.get($rootScope.config.app_ws+'jsons/json.json').success(function(response) {
            $rootScope.config.app_genres =  response;
        });

the json.json:

{ 
    "genres":{  

                "Ambient":{},
                "Alternative":{},
                "Blues":{},
                "Chillout":{},
                "Classical":{},
                "Country":{},
                "Dance":{},
                "Dubstep":{},
                "Electronic":{},
                "Folk":{},
                "Grunge":{},
                "Hip Hop":{},
                "House":{},
                "Indie":{},
                "Jazz":{},
                "Latin":{},
                "Metal":{},
                "Opera":{},
                "Pop":{},
                "Punk":{},
                "R&B":{},
                "Reggae":{},
                "Rock":{},
                "Ska":{},
                "Soul":{},
                "Vocal":{},
                "Funk":{},
                "Lounge":{},
                "Geek":{}
    }
}

the HTML:

<div ng-repeat="i in config.app_genres.genres">
<span class="span3x3" ng-if="$first"><a href="">Others</a></span>
<span class="span3x3"><a href="">{{i}}</a></span>
<div class="row-fluid" ng-if="($index%3) == 1"></div>

i'm just trying listing in html all the json genres names , so for example Alternative, Ambient, Pop, Rock, etc but actually it prints {} instead of the genre Name.

What's happening?

1
  • @steo no man really nothing in console :( Commented Jan 25, 2014 at 18:02

2 Answers 2

3

Genres is an object, not a array. This means it doen't have a list of elements, but a list of (key, value) pairs. In your case, the keys are the names of the genres and the values are empty objects.

What happens is that ng-repeat iterates by default on the values. So you have several options:

If you only need the genre names, try a JSON array instead:

{
    "Genres": [
        "Pop",
        "Rock",
        ...
        "Jazz"
    ]
}

If you need further details, iterate on both keys and values:

<div ng-repeat="(genre, details) in config.app_genres.genres">

With a JSON like:

{
    "Genres": {
        "Jazz": {
            "songs": 50
        },
        "Pop": {
            ...
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Now that I look at the JSON file it actually prints the right value. If you see, you are using an "object literal" while you should have an array instead

"Genres": ["Ambient", "Alternative", .., "Jazz"]

At the very moment, the value of every genre is actually { } which is the value that is printed in your HTML.

1 Comment

ok, but sorry what's wrong here? pastebin.com/GyQEXVGc why jsonlint won't validate it? :/

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.