2014-10-26 129 views
1

我在AngularJS應用以下配置:避免視圖(角UI路由器)在AngularJS直接訪問

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' 
       }); 
     }]); 

當用戶類型http://example.net「開始」的觀點被加載好http://example.net/#/start,但如果用戶直接鍵入http://example.net/#/quotation「報價」視圖加載,那裏我有一些問題,因爲從開始到報價的調用加載一些值(通過REST API),所以..如果用戶直接鍵入該視圖,加載是不正確。

如何避免直接訪問其他視圖?

+0

這類似於實施認證檢查。例如:http://stackoverflow.com/questions/22537311/angular-ui-router-login-authentication – akonsu 2014-10-26 16:21:38

+0

你是建立一個移動應用程序或Web應用程序? – 2014-10-26 16:21:52

+0

如果有任何異常拋出,您可以檢查每個狀態更改。如果是這樣,則通過轉到起始頁面或顯示一些錯誤消息來處理它 – dchhetri 2014-10-26 17:03:57

回答

0

在星級頁面負載變化設定$rootscope.allowpage=true然後使用下面的代碼,它會檢查$rootscope.allowpage是否被設置爲真還是假,如果$rootscope.allowpage不是真的會重定向到起始頁

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'); 
       }}); 
});