Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ui-router cannot resolve $resource results

In one of my UI-router states, I have the following link '/users', which points to a page that shows a list of users. Below is the code I wrote to resolve the list of users by using a $resource call, however, the data doesn't seem resolved when the page is loaded:

.state('users', {
                    url: '/users',
                    templateUrl: '/assets/angularApp/partials/users.html',
                    controller: 'UserListCtrl as vm',
                    resolve: {

                        users: function ($resource) {

                            return $resource('http://localhost:8080/api/users').query().$promise.then(function(data) {
                                return data;
                            });
                        }
                    }


                })

In the UserListCtrl, I have the following to assign the resolved users to vm.users so that the users can be displayed on the partial page:

function UserListCtrl($log, users) {
        var vm = this;
        vm.users = users;
}

The page, however, only displays a few empty rows without any data filled in. So I think the resolve is not working properly on my url /users, could anyone spot where might be problematic? Thanks

like image 241
TonyGW Avatar asked Dec 30 '25 15:12

TonyGW


1 Answers

Change it to:

users: function ($resource) {
    return $resource('http://localhost:8080/api/users').query().$promise;
}

Because that way you're returning the promise, and by using then(...), you're resolving the promise within and just returning data thus it won't pass it to the controller because it's returning them after the controller has loaded.

like image 80
Arber Sylejmani Avatar answered Jan 01 '26 03:01

Arber Sylejmani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!