简体   繁体   中英

Avoid direct access to view(angular-ui-router) in AngularJS

I have the following configuration in my AngularJS App:

angular
    .module('MyApp')
    .config(['$stateProvider', '$urlRouterProvider', '$locationProvider', 
        function($stateProvider, $urlRouterProvider, $locationProvider) {
            $urlRouterProvider.otherwise('start');

            $stateProvider
                .state('start', {
                    url : '/start',
                    templateUrl : 'views/start.html',
                    controller : 'AppCtrl'
                })
                .state('quotation', {
                    url : '/quotation',
                    templateUrl : 'views/quotation.html',
                    controller : 'AppCtrl'
                })
                .state('loading', {
                    url : '/loading',
                    templateUrl : 'views/loading.html'
                });
        }]);

When the user type http://example.net the "start" view is loaded good http://example.net/#/start , but if the user type directly http://example.net/#/quotation the "quotation" view is loaded and there I have some problems, because the call from start to quotation load some values (via REST API), so .. If the user type directly that view, the loaded was not correct.

How I can avoid direct access to another views?

In star page load change set the $rootscope.allowpage=true then use the following code, it will check whether $rootscope.allowpage is set to true or not , if $rootscope.allowpage not true it will redirect to start page

angular.module('MyApp'['ngRoute','ui.bootstrap','timer'])
.run(function($rootScope,$location) {
        $rootScope.$on( "$routeChangeStart", function(event, next, current) {
              if ( $rootScope.allowpage!= true ) {
                    $location.path('start');
              }});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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