为什么 $state.transitionTo 或 $state.go 不显示新的 HTML 部分? [英] Why does $state.transitionTo or $state.go does not display the new HTML partial?

查看:21
本文介绍了为什么 $state.transitionTo 或 $state.go 不显示新的 HTML 部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一段代码,用于在每次 UI 路由器状态更改之前检查用户权限.一切正常,除了当权限正常时,转换到新状态(使用 $state.go,如下所示,或使用 $state.transitionTo)似乎根本没有做任何事情(控制台消息已记录,但仅此而已).

Here is a piece of code destined to check user rights before each UI-router state change. Everything works fine, except the fact that when rights are OK, the transition to the new state (either with $state.go, as below, or with $state.transitionTo), does not seem to do anything at all (the console message is logged but that's all).

angular.module('mymodule', [ /* dependancies */ ])

.run( function($rootScope, $window, $state, AuthManager)
{
    $rootScope.$on('$stateChangeStart',
        function(event, toState, toParams, fromState, fromParams)
        {
            if( ! S.isUserConnected() && toParams.requiresLogIn )
            {
                event.preventDefault();
                AuthManager.openConnectionPane().then( function( data )
                {
                    if( AuthManager.isUserConnected() ) {
                        console.log("This message is correctly printed!");
                        $state.go( toState.name );
                    }
                });
            }
        }
    );
});

你知道为什么这不起作用吗?

Do you have any idea why this does not work?

我注意到对应于我们正在转换到的状态的 HMTL 部分是通过 GET 请求正确获取的,但它从未显示:旧的 HTML 保持显示(来自前一状态的那个),即使 URL已正确更新...

I have noticed that the HMTL partial coresponding to the state we are transitioning to, is correctly fetched via a GET request, but it never shows: the old HTML stays displayed (the one from the previous state), even if the URL is correctly updated...

添加路由器配置代码

在主模块文件中:

//angular.module('mymodule', [ /* dependancies */ ])
.config( $urlRouterProvider ) {
    $urlRouterProvider.otherwise('/home');
}

在每个子模块中:

angular.module( 'mymodule.submodule', [ /* dependancies */ ])

.config(function($stateProvider) {
    $stateProvider
        .state('events-overview', {
            url:'/events',
            templateUrl: 'app/events/events-overview.html',
            controller: 'EventsOverviewCtrl'
        })
        .state('events-details', {
            url:'/events/{eventId:int}',
            templateUrl: 'app/events/events-details.html',
            controller: 'EventsDetailsCtrl'
        })
        .state('events-creation', {
            url:'/events/new',
            params: {
                requiresLogIn: true
            }
            templateUrl: 'app/events/events-creation.html',
            controller: 'EventsCreationCtrl'
        })
});

推荐答案

我遇到了类似的问题,我是这样解决的:

I had a similar issue and this is how I solved it:

if( AuthManager.isUserConnected() ) {
   //...
   event.preventDefault();
   $state.go(toState.name, null, {notify: false}).then(function (state) {
      $rootScope.$broadcast('$stateChangeSuccess', state, null);
   });
}

正如其他答案中提到的,$state.go.run 中无法正常工作.按照这个线程的建议,我最终使用了这个解决方法>

As it's mentioned in other answers, $state.go doesn't work propertly in .run. Following the advises of this thread I ended up working with this workaround

$urlRouterProvider.otherwise(function ($injector) {
   var $state = $injector.get('$state');
   $state.go('/home');
});

不确定这是否可以帮助您,但您可以尝试一下.希望有帮助

Not sure if this could help you, but you could try it. Hope it helps

这篇关于为什么 $state.transitionTo 或 $state.go 不显示新的 HTML 部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆