Article From:https://segmentfault.com/q/1010000011078563
Question:

$stage.go()Direct dead cycle, always in stateChangeStart, page crash.

app.js

define(['routes', 'util', 'services/Interceptor', 'angular-ui-router'], function (config, util, Interceptor) {

    var app = angular.module('app', ['ui.router', 'ng-progress']);

    app.config(
        [
            '$locationProvider',
            '$controllerProvider',
            '$compileProvider',
            '$filterProvider',
            '$provide',
            '$stateProvider',
            '$urlRouterProvider',
            '$httpProvider',
            '$httpProgressOpsProvider',

            function ($locationProvider, $controllerProvider, $compileProvider, $filterProvider, $provide, $stateProvider, $urlRouterProvider, $httpProvider, $httpProgressOpsProvider) {
                app.controller = $controllerProvider.register;
                app.directive = $compileProvider.directive;
                app.filter = $filterProvider.register;
                app.factory = $provide.factory;
                app.service = $provide.service;

                //$locationProvider.html5Mode(true);

                if (config.states !== undefined) {
                    angular.forEach(config.states, function (states, path) {
                        $stateProvider.state(states.name, states.data);
                    });
                }

                // if (config.defaultRoutePath !== undefined) {
                //     $urlRouterProvider.otherwise({ redirectTo: config.defaultRoutePath });
                // }

                $urlRouterProvider.otherwise('/');

                //Exception Interceptor$httpProvider.interceptors.push (Interceptor.exception);//httP requests interception, progress bar$httpProgressOpsProvider.setOps ({Background:'#6435c9',StartAt: 30,AutoPauseAt: 75,//httpFalse,//increment: 0.5,//duration: 100});}])App.run (['$rootScope','$location','$state', function ($rootSco)PE, $location, $state) {$rootScope.$on ('$stateChangeStart', function (event, toState, toPar)AMS, fromState, fromParams) {/ / judge logonIf (! $rootScope.user) {/ / cancel default jump behaviorEvent.preventDefault ();/ / jump$state.go ('App.warn_rule_set');}Console.log (event, fromState, toState, $state);$rootScope.currentStateDesc = toState.desc null;});]]))Return app;});

route.jsRouting configuration

define(['services/DependencyResolver'], function (dependencyResolverFor) {
    var loadController = function (controllerName) {
        return ["$q", function ($q) {
            var deferred = $q.defer();
            require([controllerName], function () {
                deferred.resolve();
            });
            return deferred.promise;
        }];
    };

    return {
        defaultRoutePath: '/',
        states: [
            {
                name: "app",
                data: {
                    url: '/',
                    views: {
                        '': {
                            templateUrl: '/views/common/wrapper.html',
                            controller: 'MainController'
                        },
                        'content@app': {
                            templateUrl: '/views/common/content.html',
                            controller: null
                        }
                    },
                    resolve: {
                        MainController: loadController("controllers/MainController")
                    }
                }
            },
            {
                name: "app.warn_rule_set",
                data: {
                    url: 'warn_rule_set',
                    desc: 'Alarm rules / settings',Views: {'content': {TemplateUrl:'/views/warn/rule_set.html',Controller:'WarnRuleSetCoNtroller',ControllerAs:'vm'}},Resolve: {WarnRuleSetController: loadController ("Controllers/WarnRuleSetController")}}},{Name: "app.warn_rule_list",Data: {Url: 'Warn_rule_list',Desc: 'alarm rule / list',Views: {'content': {TemplateUrl:'/views/warn/rule_list.html',Controller:'WarnRuleListController',ControllerAs: 'Vm'}},Resolve: {WarnRuleListController: loadController ("controllers/WarnRuleListController")}}},{Name: "app.warn_rule_detail",Data: {Url:'warn_rule_detail',Desc: 'alarm rule/ details',Params: {pWarnRule: null},Views: {'content': {TemplateUrl:'/views/warn/rule_detail.html',Controller:'WarnRuleDetailController',ControllerAs:'vm'}},Resolve: {WarnRuleDetailController: loadController ("controllers/WarnRuleDetailController")}}},{Name: "app.warn_sms_sEt ",Data: {Url:'warn_sms_set',Desc: 'SMSPolice / settings',Views: {'content': {TemplateUrl:'/views/warn/sms_set.html',Controller:'SmsWarnSetControlLer',ControllerAs:'vm'}},Resolve: {SmsWarnSetController: loadController ("contrOllers/SmsWarnSetController ")}}},{Name: "app.warn_sms_list",Data: {Url:'warn_sms_list',Desc: 'SMS alarm / list',Views: {'cOntent': {TemplateUrl:'/views/warn/sms_list.html',Controller:'SmsWarnListController',ControllerAs:'vm'}},Resolve: {SmsWarnListController: loadController ("controllers/SmsWarnListController")}}},{Name: "app.warn_sms_detail",Data: {Url:'warn_sms_detail',Desc: 'SMS alarm / details',Params: {pSmsWarn: null},Views: {'content': {TemplateUrl:'/views/warn/sms_detail.html',Controller:'SmsWarnDetailController',ControllerAs:'vm'}},Resolve: {SmsWaRnDetailController: loadController ("controllers/SmsWarnDetailController")}}},{Name: "app.warn_mail_set",Data: {Url:'warn_mail_set',Desc: 'mail alarm / setup',Views: {'content': {TemplateUrl:'/viEws/warn/mail_set.html',Controller:'MailWarnSetController',ControllerAs:'vm'}},Resolve: {MailWarnSetController: loadController ("controllers/MailWarn")SetController ")}}},{Name: "app.warn_mail_list",Data: {Url:'warn_mail_list',Desc: 'mail alarm / list',Views: {'content': {TemplateUrl:'/views/warn/mail_list.html',CoNtroller:'MailWarnListController',ControllerAs:'vm'}},Resolve: {MailWarnListCOntroller: loadController ("controllers/MailWarnListController")}}},{Name: "app.warn_mail_detail",Data:{Url:'warn_mail_detail',Desc: 'mail alarm / details',Params: {pMailWarn: null},Views: {'content': {TemplateUrl:'/views/warn/mail_detail.html',Controller:'MailWarnDetailController',ControllerAs:'vm'}},Resolve: {MailWarnDetailController: loadController ("controllers/MailWarnDetailController")}}},{Name: "app.query_component",Data: {Url:'query_component',Desc: 'query Association / component',Views: {'content': {TemplateUrl:'/views/query/component.html',Controller:'QueryComponentController',ControllerAs:'vm'}},Resolve: {QueryComponentController: loadController ("controllErs/QueryComponentController ")}}},{Name: "app.query_core",Data: {Url:'query_core',Desc: 'query Association / kernel',Views: {'contenT': {TemplateUrl:'/views/query/core.html',Controller:'QueryCoreController',ControllerAs:'vm'}},Resolve: {QueryCoreContRoller: loadController ("controllers/QueryCoreController")}}},{Name: "app.query_software",Data: {Url:'query_software',Desc: 'query Association / software',Views: {'content': {TemplateUrl:'/views/query/softWare.html',Controller:'QuerySoftwareController',ControllerAs:'vm'}},ResolVe: {QuerySoftwareController: loadController ("controllers/QuerySoftwareCont")Roller ")}}},{Name:"App.query_integrity",Data: {Url:'query_integrity',Desc: 'query Association / integrity',Views: {'content': {TemplateUrl:'/views/query/integrity.html',ContrOller:'QueryIntegrityController',ControllerAs:'vm'}},Resolve: {QueryIntegrityController: loadController ("controllers/QueryIntegrityController")}}},{Name: "app.query_attack",Data:{Url:'query_attack',Desc: 'query Association / attack',Views: {'content': {TemplateUrl:'/views/queRy/attack.html',Controller:'QueryAttackController',ControllerAs:'vm'}},ReSolve: {QueryAttackController: loadController ("controllers/QueryAttackContr")Oller ")}}},]};});

Answer 0:

$stateChangeStartYou will capture all the routing events, you are here in the $rootScope registration event, all of the routing changes in the $rootScope will be captured, you write the jump logic to hook affirmative, definitely dead cycle.

The solution is not to register on $rootScope, to register in the scope with special control rights, and to upgrade to the version of ui-router 1.x, on $stateThis kind of hook, the official document has suggested using the $transitions service instead, because it is more flexible and can satisfy some $state hookThe situation that can’t be satisfied.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *