三、angularjs 如何在頁面沒有登入的情況下阻止使用者通過更改url進入頁面--$stateChangeStart

來源:互聯網
上載者:User

標籤:pos   mst   ram   test   本地   方法   function   ext   token   

有時候使用者沒有登入或者在某些情況下你是不希望使用者進入頁面,但是angular的路由機制可以讓使用者直接通過更改Url進入頁面,如何處理這一問題呢?——監控路由轉換機制 $stateChangeStart具體的操作如下:
   //監控路由轉換        $rootScope.$on(‘$stateChangeStart‘, function(event, toState, toParams, fromState, fromParams) {//上面的參數是固定的            if(toParams.name === ‘quanCheck‘ && !$sessionStorage.userInfo) {     //進入O2O卡券驗證頁面                event.preventDefault();//該方法是阻止頁面進入先進行下面的判斷                //驗證token資訊是否合法,合法則跳到卡券驗證介面                Service.post(‘users‘, ‘verifyToken‘, { sysId: systemId.o2O, token: toParams.param }).then(function(data) {                    $sessionStorage.userInfo = angular.extend(data.dataInfo, { currentSystemId: systemId.o2O });                    $state.go(‘o2O‘, { parent: ‘verify‘, name: ‘quanCheck‘, param: null });                }, function(error) {                    $state.go(‘o2OLogin‘);                });            } else if(notCheckLoginStates.indexOf(toState.name) < 0 && !$sessionStorage.userInfo) {     //防止使用者不登入直接進入功能模組                event.preventDefault();                $state.go(localStorage.defaultLoginState || ‘chooseSystem‘);                swal(‘請先‘ + (localStorage.defaultLoginState ? ‘登入!‘ : ‘選擇系統!‘), ‘‘, ‘error‘);            }            //刪除會話儲存資料            if(deleteStorageStates.indexOf(toState.name) >= 0) {                delete $sessionStorage.userInfo;                delete $sessionStorage.systemInfo;            }            //刪除本地預設登入資料            if(toState.name === ‘chooseSystem‘) {                localStorage.removeItem(‘defaultLoginState‘);               }        });

 

三、angularjs 如何在頁面沒有登入的情況下阻止使用者通過更改url進入頁面--$stateChangeStart

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.