在角度js中禁用页面刷新的路由

时间:2017-07-31 08:55:32

标签: html angularjs angular-routing route-provider

我有这样的路线提供者

app.config(function ($routeProvider, $locationProvider){
$locationProvider.hashPrefix('');


$routeProvider
.when('/', {
    templateUrl: 'login.html',
    controller: 'loginCtrl'
})
.when('/home', {
    resolve:{
        "check":function($location, $rootScope){
            if(!$rootScope.loggedIn){
                $location.path('/');
            }
        }
    },
    templateUrl:'home.html',
    controller: 'homeCtrl'
})
.otherwise({
    redirectTo: '/'
   });
});

login.html是我的应用的第一页。

但登录后,重新加载任何将在login.html页面中结束的页面

我希望其他网页在刷新和login.html上保持活力作为我的首页

2 个答案:

答案 0 :(得分:1)

重新加载页面每次都会重新创建$ rootScope。因此,您需要将登录详细信息存储在任何存储中,例如localstorage。

http://blog.teamtreehouse.com/storing-data-on-the-client-with-localstorage

此链接可能对您有所帮助。您需要在成功登录后存储数据。并获取存储的数据并在解析URL时验证使用情况。

答案 1 :(得分:0)

scotchApp.config(function($stateProvider, $urlRouterProvider, $compileProvider, $locationProvider) {
        $locationProvider.html5Mode(true);
        $compileProvider.debugInfoEnabled(false);
        // route for the home page

        $stateProvider
            .state('home', {
                url: '/home',
                templateUrl : 'pages/home.html',
                controller  : 'mainController'
            })

            // route for the about page
            .state('about', {
                url: '/about',
                templateUrl : 'pages/about.html',
                controller  : 'aboutController'
            })

            // route for the contact page
            .state('contact', {
                url: '/contact',
                templateUrl : 'pages/contact.html',
                controller  : 'contactController'
            });

            $urlRouterProvider.otherwise('home');
    });

尝试这样的事情。

相关问题