this.getData = function () {
    $http.get("data/samples.json").then(function(d) {
        console.log(angular.fromJson(d.data.cards)[0].title);
        return angular.fromJson(d.data.cards);
    });
};
this.cards = this.getData();

It prints the proper value to the console.

<p>{{main.cards[0].title}}</p>

It doesn't display anything, why?

up vote 0 down vote accepted

The returned object is async, and not returning the result (can't return within a callback). Try this:

 this.getData = function () {
    $http.get("data/samples.json").then(function(d) {
        this.cards = angular.fromJson(d.data.cards);
    });
};

Try this:

var me = this;
this.getData = function () {
    $http.get("data/samples.json").then(function(d) {
        console.log(angular.fromJson(d.data.cards)[0].title);
        me.cards = angular.fromJson(d.data.cards);
    });
};

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.