R
R
Ruslan2017-06-08 17:58:50
JavaScript
Ruslan, 2017-06-08 17:58:50

Why is Restangular not loading data?

I'm trying to load data using Restangular, as a result, the array in which I save the received data is empty.
Factory

function dataService(Restangular) {
        Restangular.setBaseUrl('http://beta.json-generator.com/api/json/get/EkKWgIpZ7?indent=4');

        var factory = {
            getPostsList: getPostsList
        };
        return factory;

        function getPostsList() {
            return Restangular.all('posts').getList();
        }

Controller
function ExampleCtrl(dataService) {
        var self = this;
        this.posts = [];
        this.getList = getList;

        function getList(){
            dataService.getPostsList.then(function(posts){
                self.posts = posts;
            },function(err){
                console.log(err);
            });
        }
        console.log(self.posts);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
ozknemoy, 2017-06-10
@ozknemoy

console.log(self.posts) fires before the promise returns and shows this.posts = []

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question