UI-Router:我可以在子状态下解析父承诺吗?

时间:2021-07-11 12:49:45

I'm working on social application which has a cover in the profile page. Both "Edit" and "Visit" page has the cover, but different sub-sections. The problem is that some controllers shown in the cover strictly depends by the section you're currently visiting.

我正在开发社交应用程序,它在个人资料页面中有封面。 “编辑”和“访问”页面都有封面,但有不同的子部分。问题是封面上显示的某些控制器严格取决于您当前访问的部分。

To solve this issue, I'm trying to return a promise in the resolve section of the parent state and then resolve the promise in the different child states.

为了解决这个问题,我试图在父状态的resolve部分中返回一个promise,然后在不同的子状态中解析promise。

.state('profile', {
    url: '/profile',
    controller: 'ProfileController',
    templateUrl: 'components/profile/profile.html',
    resolve: {
        actor: function(UserService){
            return UserService.getUser();
        },
        target: function($q){
            var deferred = $q.defer();
            return deferred.promise;
        }
    }
})
.state('profile.view', {
    url: '/:userId',
    templateUrl: 'components/profile/view.html',
    navbar: true,
    parent: 'profile',
    resolve: {
        /**
         *
         * @param target
         * @param {UserService} UserService
         */
        target: function(target, UserService){
            target.resolve(UserService.getActions('view'));
        }
    }
})

Unfortunately, there's something wrong in my logic because the return deferred.promise inside the profile state is blocking the application.

不幸的是,我的逻辑出现了问题,因为在配置文件状态中返回deferred.promise会阻止应用程序。

Could anyone help me to figure out what I'm doing wrong?

谁能帮我弄清楚我做错了什么?

Best!

1 个解决方案

#1


0  

Return the promise to the resolver function

将promise发送给解析器函数

.state('profile.view', {
    url: '/:userId',
    templateUrl: 'components/profile/view.html',
    navbar: true,
    parent: 'profile',
    resolve: {
        /**
         *
         * @param target
         * @param {UserService} UserService
         */
        target: function(target, UserService){
            return UserService.getActions('view').$promise;
        }
    }
})

This answer assumes that UserService is a $resource query. Other APIs return promises differently but the principle is the same. Return the promise to the resolver function.

此答案假定UserService是$ resource查询。其他API以不同的方式返回承诺,但原则是相同的。将promise发送给解析器函数。

#1


0  

Return the promise to the resolver function

将promise发送给解析器函数

.state('profile.view', {
    url: '/:userId',
    templateUrl: 'components/profile/view.html',
    navbar: true,
    parent: 'profile',
    resolve: {
        /**
         *
         * @param target
         * @param {UserService} UserService
         */
        target: function(target, UserService){
            return UserService.getActions('view').$promise;
        }
    }
})

This answer assumes that UserService is a $resource query. Other APIs return promises differently but the principle is the same. Return the promise to the resolver function.

此答案假定UserService是$ resource查询。其他API以不同的方式返回承诺,但原则是相同的。将promise发送给解析器函数。