0

I've spent a few hours on this and I just can't get the array data to display on the page. I'm not getting any errors.

     (function() {
        var app = angular.module('myApp', []);

        app.controller('AppController', ['$http', function($http){
            this.files = [];

            $http.get('angular-data.json').success(function(data){
                this.files = data;
            });
        }]);
    })();

And inside my angular-data.json file:

[{"name": "myfile.doc","ext": "doc","size": "168KB","modified": "5 mins"}]

Someone please help :-(

3
  • 1
    Please provide your HTML and don't use .success Commented May 4, 2016 at 5:00
  • The code worked fine when I had the array in the page, so I'm guessing there's some issue with the way I'm calling the JSON data into the array. Commented May 4, 2016 at 5:01
  • @DanielWilliams Have you got a chance to try my answer. I really think this is your problem. Commented May 13, 2016 at 12:12

3 Answers 3

1

Convert your .success to .then

Then convert this

this.files = data;

to this

this.files = data.data;
Sign up to request clarification or add additional context in comments.

2 Comments

Regrettably, I get the same result - nothing displayed.
Can you provide output of this console.log(data.data) and you are using .then right ? @DanielWilliams
0

Try this

this.files = data[0];

In angular-data.json file it have object in array

1 Comment

That will only ever allow one array item.
0

I think the this in success callback is not getting its context.

Try binding it in $scope or if you are using vm, just like

for $scope

(function() {
    var app = angular.module('myApp', []);

    app.controller('AppController', ['$http', '$scope', function($http, $scope){
        $scope.files = [];

        $http.get('angular-data.json').success(function(data){
            $scope.files = data;
        });
    }]);
})();

For vm

(function() {
    var app = angular.module('myApp', []);

    app.controller('AppController', ['$http', function($http){
        var vm = this;
        vm.files = [];

        $http.get('angular-data.json').success(function(data){
            vm.files = data;
        });
    }]);
})();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.