简体   繁体   English

UI路由器FromState始终为^,无限循环

[英]Ui-router FromState is always ^, infinite loop

I'm only calling $state.go once. 我只给$ state.go打电话一次。 After the first call to $state.go, the page keeps refreshing itself and calling $stateChangeStart. 在第一次调用$ state.go之后,页面会不断刷新并调用$ stateChangeStart。 I'm suspecting it has something to do with the StateProvider configuration. 我怀疑它与StateProvider配置有关。 Any pointers as to what could be wrong? 关于可能出什么问题的任何指示?

Here's a gif of the problem, and the code I think is the culprit below: http://i.giphy.com/3o85xGdxHnkanVecgM.gif 这是问题的gif图像,我认为是下面的原因: http : //i.giphy.com/3o85xGdxHnkanVecgM.gif

angular.module('ticketingSystemApp', ['ui.router', 'ngCookies', 'ticketingSystemApp.filters', 'ticketingSystemApp.directives', 'ticketingSystemApp.controllers', 'ticketingSystemApp.services'])
.run(['$state',  '$rootScope', function ($state, $rootScope) {
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        alert("ToState=" + toState.name + " FromState=" + fromState.name + " url=" + fromState.Url);

        if (($rootScope.username == null || $rootScope.password == null) && toState.name !== "login") {
            $state.go("login");
            e.preventDefault();
        }  else if ($state.name === "") {
            $state.go('userpanel');
            e.preventDefault();
        } 
    });
    }
]);



    .config([
        '$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {
            $locationProvider.hashPrefix('!').html5Mode(true);
            // UI States, URL Routing & Mapping. For more info see: https://github.com/angular-ui/ui-router
            // ------------------------------------------------------------------------------------------------------------
            $stateProvider
                .state('login', {
                    url:'/login',
                    views: {
                        "mainContent": {
                            templateUrl: '/Account/Login'
                        }
                    }

                })
                .state('userpanel', {
                    url:'/userpanel',
                    views: {
                        "mainContent": {
                            templateUrl: '/UserPanel'
                        }
                    }

                });

            //We don't know where we are. Go to user panel.
            $urlRouterProvider.otherwise(function ($injector, $location) {
                var $state = $injector.get('$state');

                if ($state.name === undefined) {
                    $state.go('userpanel');
                    return;
                }

            });
        }
    ])

Stupid mistake - if you're encountering this problem, remember that calling TemplateUrl on a server side page will cause that server side page to render, which in turn will call the master page to render. 愚蠢的错误-如果遇到此问题,请记住在服务器端页面上调用TemplateUrl将导致该服务器端页面呈现,而后者又将调用母版页面呈现。

So if you define your angular scripts in the master page, using code like the above will break it. 因此,如果您在母版页中定义了角度脚本,则使用上面的代码会破坏它。

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

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