Zugriff auf geschachtelte JSON-Objekt im AngularJS-controller

Ich bin neu in AngularJS und versuchen, um eine $scope für tracks für eine spätere Verwendung

Daten.json (Beispiel):

[
{
    "album": "Album name",
    "tracks": [
        {
            "id": "1",
            "title": "songtitle1",
            "lyric": "lyrics1"
        },
        {
            "id": "2",
            "title": "songtitle2",
            "lyric": "lyrics2"
        }
    ]
}
]

Controller

app.controller('lyricsCtrl', function($scope, $http) {
$http.get('data.json')
    .then(function(result) {
        $scope.albums = result.data;
        $scope.tracks = result.data.tracks;

        console.log($scope.tracks);  //Undefined...
    });
});

Warum ist $scope.tracks undefined?

Sieht aus wie Ihre data ist ein array von Objekten, die enthalten track und album. Also, so etwas wie würde für dein Beispiel: result.data[0].tracks

InformationsquelleAutor David | 2015-03-04

Schreibe einen Kommentar