角UI路由器无法解析$资源结果路由器、结果、资源、UI

2023-09-13 05:01:20 作者:思慕

在我的UI路由器的国家之一,我有以下链接/用户,它指向一个页面,显示用户的列表。下面是code我写了使用$资源调用,以解决用户的列表中,但是数据似乎并不被加载页面时解析:

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;
                            });
                        }
                    }


                })

在 UserListCtrl ,我有以下的解决用户分配给vm.users,使用户可以在部分页面上显示:

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

推荐答案

将其更改为:

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.