module.run()和$ state.go()angularJS [英] module.run() and $state.go() angularJS

查看:288
本文介绍了module.run()和$ state.go()angularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能有某种missunderstanding的。使用im角UI路由器和我有下一个问题:

I might have some kind of missunderstanding. Im using angular ui router and i have the next issue:

我有一个状态提供商:

angular
.module('MainApp')
.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider ) {
    $stateProvider
        .state('register', {
            url: "/",
            abstact: true,
            views: {
                "side-bar": {
                    controller: "GetSideBarCtrl"
                },
                "page-body@register": {
                    templateUrl: "/App/Views/WelcomePage.html"
                }
            },
        })
        .state('register.Register', {
            url: "Register",
            views: {
                "page-body": {
                    controller: "RegisterCtrl",
                    templateUrl: "/App/Views/Register.html"
                }
            },
        })
        .state('register.Login', {
            url: "Login",
            views: {
                "page-body": {
                    controller: "LoginCtrl",
                    templateUrl: "/App/Views/Login.html"
                }
            },
        })
        //For registered and loged in users!
    /* */
        .state('main', {
            url: "/:userId",
           // abstact: true,
            views: {
                "side-bar": {
                    controller: "GetSideBarCtrl"
                },
                "page-body@main": {
                  //  controller: "LoginCtrl",
                    templateUrl: "/App/Views/MainPage.html"
                }
            },
        });
    $urlRouterProvider.otherwise('/');
}]);

和运行功能

angular
.module('MainApp')
.run(['$rootScope', '$state', '$cookieStore', 'principal',
function ($rootScope, $state, $cookieStore, principal) {
    var cookies = $cookieStore.get('user');
    if (cookies) {
        principal.Authenticate(cookies.split(' ')[0], cookies.split(' ')[1]);
        $state.go('main', { userId: cookies.split(' ')[0] });
    } else {
        $state.go('register.Login');
    }
}]);

一切工作正常,当cookies是present和authenication工作没有任何问题,但$ state.go什么都不做,我不能找出原因,你可以请帮和我解释...

Everything works fine, when cookies are present and authenication works without any problems but $state.go does nothing, i cant figure out why, can you please help and explain me...

推荐答案

是的,所建议的它看起来像$ state.go()不module.run正常工作()。

Yep, as suggested it looks like $state.go() doesn't work correctly in module.run().

我发现,把$状态code在$超时工作,例如

I found that putting the $state code in a $timeout works, e.g.

angular.module('MainApp').run($timeout, $state, ...) {

    $timeout(function() {
        $state.go('main');
    });

}

这篇关于module.run()和$ state.go()angularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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