1

I've got no HTML output but when I console.log the result of my $http.get I've got the object I want to have. Can someone explain me how to get the data from $http.get in my template?

.state('secure/contacts',{
                url:'/secure/contacts',
                template:"Contacts: {{contacts  | json}}",
                resolve:{
                    contacts : function(UserService){
                        return UserService.all();
                    }
                },
                controller:function($scope,contacts){

                    $scope.contacts = contacts;


                }
            })

.service('UserService',function($http){
   return {
       get:get,
       all:all
    } ;

    function all(){
        $http.get('/api/contacts').then(function(payload){
            console.log(payload.data);
            return payload.data;
        });
    }

    function get(id){
        return $http.get('/api/user/'+id).then(function(payload){
            return payload.data;
        });
    }

});

2 Answers 2

2

Your all() function doesn't return anything.

Replace

function all(){
    $http.get('/api/contacts').then(function(payload){
        console.log(payload.data);
        return payload.data;
    });
}

by

function all(){
    return $http.get('/api/contacts').then(function(payload){
        console.log(payload.data);
        return payload.data;
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to return promise (return $http.get(...)) from UserService.all method:

function all() {
    return $http.get('/api/contacts').then(function(payload) {
        return payload.data;
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.