AngularJS: How to load JSON data onto a scope variable

后端 未结 5 1468
南笙
南笙 2021-02-07 21:46

I\'m creating a personal website where I can keep updating content without having to manipulate the HTML. I\'m trying to achieve this by simply loading and updating

5条回答
  •  抹茶落季
    2021-02-07 22:18

    Your json file has an array with the first and only element in the array being a json object. When .success() fires, and data is passed into the lambda function, data is an array, not just json. All you have to do is access the zeroth element of the array.

    So this:

    .success(function(data) {
        $scope.contents = data;
    })
    

    Should be this:

    .success(function(data) {
        $scope.contents = data[0];
    })
    

    Also, you should check data[0] to make sure that it's json, and if it doesn't validate, you should probably call parseJSON(data[0]) on it.

提交回复
热议问题