繁体   English   中英

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

[英]UI-Router: Can I resolve a parent promise in a child state?

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

为了解决此问题,我试图在父状态的“ 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'));
        }
    }
})

不幸的是,我的逻辑有问题,因为配置文件状态内的return deferred.promise阻止了应用程序。

有人可以帮我弄清楚我在做什么错吗?

最好!

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

该答案假定UserService$resource查询。 其他API返回的诺言有所不同,但原理相同。 将promise 返回到解析程序功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM