在UI-Router中调用子状态时触发无限循环

时间:2015-11-11 20:44:36

标签: javascript angularjs nested angular-ui-router state

我在UI-Router的父控制器中有以下配置。

$stateProvider.state('store', {
    abstract: true,
    url: '/:store',
    resolve: {
        Store : ['StoreService', '$stateParams', '$state', '$rootScope', function (StoreService, $stateParams, $state, $rootScope) {

            return StoreService.getStoreByCode($stateParams.store).then(
                function (response){
                    $rootScope.Store = response.data; 
                    return response.data;
                },
                function (response){
                    $state.go('store.404', $stateParams);
                }
            );
        }]
    },
    templateUrl: 'app/app.html'
});

然后在子配置中我有:

$stateProvider
    .state('store.404', {
        parent:'store',
        url: '/store.404',
        template: '<h1>Whoa! 404!</h1>'
    });

问题在于,当resolve语句中的promise被拒绝时,$state.go('store.404')触发并且无限循环并崩溃浏览器。

这是因为父母每次尝试进入该子状态时都在加载和解析吗?是否有一种干净的方法可以在子状态上没有解析语句触发器?

1 个答案:

答案 0 :(得分:1)

  

...这是因为父母每次尝试进入该子状态时都在加载和解析吗?

  

...是否有一种干净的方法可以在子状态上没有解析语句触发器?

没有

解决方案是 - 像#404;&#39;应该是独立的,特别的。这样,总是可以使用,没有任何限制或错误处理。

即。 - 移动&#39; 404&#39;任何父母以外的州。