检查父状态承诺是否已返回值

使用AngularJS和ui-router创建状态,我有一个父状态和子状态。


    .state('portfolio', {

        url: '/portfolio',

        templateUrl: 'app/templates/portfolio/portfolio.tpl.htm',

        controller: 'portfolioCtrl',

        controllerAs: '$ctrl'

    })

    .state('portfolio.patent', {

        url: '/:patentId',

        views:{

            "": {

                controller: 'caseOverviewCtrl',

                controllerAs: '$ctrl',

                templateUrl: 'app/templates/patent/case-overview.tpl.htm',

            },

            //FURTHER SIBLING VIEWS

       }

     })

在portfolio控制器中,我发出请求,等待承诺,然后在表中向用户显示数据。如果用户从表中选择一个项目,它会在子 state 中显示更多信息portfolio.patent,将 id 值传递给$stateParams,然后我使用它来进行 $http 调用以获取更多信息。


如果我刷新页面,子状态将显示在父状态之前,因为父状态 $http 请求需要更长的时间来解析,因为要获取更多数据。我试图从子状态检查 的值,portfolioLoaded但它只检查一次。


问题

在向用户显示子状态之前,如何检查父状态承诺已解决?


我看到ng-show在 `portfolio.patent' 视图中使用来检查控制器是否已解决父承诺。


投资组合控制器

var promise = patentsRestService.fetchAllPatents();

promise.then(

    function(response){

        var patents = response;

        $scope.portfolioLoaded = true;

    }

)

投资组合.专利控制人

function init() {

    if($scope.$parent.portfolioLoaded) {

        $scope.parentLoaded = true;

    }

}

专利视图

<div data-ng-show="$ctrl.portfolioLoaded" class="animate-show">

   //CONTENT

</div>


白衣非少年
浏览 136回答 2
2回答

郎朗坤

将承诺保存在父控制器中:$scope.promise = patentsRestService.fetchAllPatents();$scope.promise.then(&nbsp; &nbsp; function(response){&nbsp; &nbsp; &nbsp; &nbsp; var patents = response;&nbsp; &nbsp; &nbsp; &nbsp; $scope.portfolioLoaded = true;&nbsp; &nbsp; })在子控制器中使用承诺:function init() {&nbsp; &nbsp; $scope.$parent.promise.then(function() {&nbsp; &nbsp; &nbsp; &nbsp; $scope.parentLoaded = true;&nbsp; &nbsp; });}这将适当延迟设置。

慕村9548890

将父状态的调用移动到resolve:.state('portfolio', {&nbsp; &nbsp; url: '/portfolio',&nbsp; &nbsp; templateUrl: 'app/templates/portfolio/portfolio.tpl.htm',&nbsp; &nbsp; controller: 'portfolioCtrl',&nbsp; &nbsp; controllerAs: '$ctrl',&nbsp; &nbsp; resolve: {&nbsp; &nbsp; &nbsp; &nbsp; patentData: function(patentsRestService){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return patentsRestService.fetchAllPatents()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }})它总是会在子状态加载之前解决阅读更多:https : //github.com/angular-ui/ui-router/wiki#resolve
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript