UI-Router $ state不会更改URL或状态

时间:2014-06-30 23:13:21

标签: javascript angularjs angular-ui-router

以下是我的states的配置页:

//
// THESE ARE THE GUEST ACCESS LEVEL AND ROUTES
// 
var guest = [];

guest.push({
    state: 'guest',
    abstract: true,
    url: '^',
    templateUrl: 'app/views/layouts/guest.html',
    data: {
        access: accessLevels.guest
    }
});
guest.push({
    state: 'guest.index',
    url: '',
    template: 'Nothing to see here',
});

guest.push({
    state: 'guest.login',
    url: '/login/',
    template: '<login-panel/>',
    dependencies: [
        'app/components/login/app'
    ]
});

guest.push({
    state: 'guest.indexHash',
    url: '/',
    template: 'Nothing to see here',
});


guest.push({
    state: 'guest.logout',
    url: '/logout/',
    controller: function($state, $http) {
        console.log('delete');
        $http.delete('/api/sessions')
            .success(function() {
                $state.go('guest.index');
            });
    }
});

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

//
// THESE ARE THE USER ACCESS LEVEL AND ROUTES
// 
var user = [];

user.push({
    state: 'user',
    abstract: true,
    templateUrl: 'app/views/layouts/user.html',
    data: {
        access: accessLevels.user
    }
});
user.push({
    state: 'user.home',
    url: '^/home/'
});

var schedule = [];
schedule.push({
    state: 'user.schedule',
    abstract: true,
    url: '^/schedules/',
    views: {
        'leftView': {
            template: '<div ui-view="leftView"/>'
        },
        'mainView': {
            template: '<div ui-view="mainView"/>'
        },
        'rightView': {
            template: '<div ui-view="rightView"/>'
        }
    },
    dependencies: [
        'app/components/schedule/app'
    ]
});
schedule.push({
    state: 'user.schedule.blank',
    url: '',
    controller: function($state) {
        console.log('Transit');
    }
});
schedule.push({
    state: 'user.schedule.view',
    url: ':day/:month/:year/',
    views: {
        'mainView': {
            template: '<schedule/>'
        }
    }
});
schedule.push({
    state: 'user.schedule.edit',
    url: ':day/:month/:year/edit/',
    views: {
        'mainView': {
            template: '<schedule/>'
        }
    }
});
user = user.concat(schedule);

var routes = guest.concat(user);
return {
    defaultRoutePath: '/',
    routes: routes
};

在我的app.js run()函数中,我想添加一些身份验证代码,到目前为止我还有:

$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) {
    if (!Auth.authorize(toState.data.access)) {
        console.warn('no authorize');
        $state.go('guest.login');
    }
});

当我运行上面的代码时,警告会显示,但页面没有变化;内容仍然相同,并且URL仍然与以前相同。

我在app.config注册路线:

// Link the routes and resolve the dependencies
        if (config.routes !== undefined) {
            _.each(config.routes, function(route) {
                $stateProvider.state(route.state, {
                    url: route.url,
                    templateUrl: route.templateUrl,
                    template: route.template,
                    views: route.views,
                    data: route.data,
                    abstract: route.abstract,
                    controller: route.controller,
                    resolve: dependencyResolver(route.dependencies)
                });
            });
        }

0 个答案:

没有答案