2017-07-04 28 views
0

我試圖在ionic1的應用中添加後退按鈕導航,我已經加入代碼離子的Android後退按鈕導航到上一個頁面無法正常工作

$ionicPlatform.registerBackButtonAction(function (event) { 
     event.preventDefault(); // Back button will DO NOTHING. 
     //alert("Going back!!!"); 
    }, 100); 

註冊.run(function($ionicPlatform)的 後退按鈕,並阻礙了Android的後退按鈕關閉應用程序,但我無法轉到上一頁。到目前爲止,我已經試過

$ionicHistory.goBack(); 

$state.go('menu.home'); 

,但他們不工作。每次按下返回按鈕時,我怎樣才能調用前一頁?

下面是一些國家從routes.js:

angular.module('app.routes', []) 

.config(function($stateProvider, $urlRouterProvider) { 

    $stateProvider 


     .state('menu.home', { 
    url: '/page1', 
    views: { 
     'side-menu21': { 
     templateUrl: 'templates/home.html', 
     controller: 'homeCtrl' 
     } 
    } 
    }) 

    .state('menu', { 
    url: '/side-menu21', 
    templateUrl: 'templates/menu.html', 
    controller: 'menuCtrl' 
    }) 

    .state('menu.policySummaries', { 
    url: '/page4', 
    views: { 
     'side-menu21': { 
     templateUrl: 'templates/policySummaries.html', 
     controller: 'policySummariesCtrl' 
     } 
    } 
    }) 
$urlRouterProvider.otherwise('/side-menu21/page1') 


}); 
+0

你試過'窗口.history.back();' – Edison

+0

@Edison哇是天才,它在android模擬器中工作。 如何確保在主頁按下後退按鈕時關閉應用程序。我的主頁的狀態是: '.state('menu.home',{ url:'/ page1', views:{ 'side-menu21':{ templateUrl:'templates/home.html' , 控制器:'homeCtrl' } } })' –

回答

0

這部作品只是確保注入$狀態和$ ionicPopup在.RUN功能:

$ionicPlatform.registerBackButtonAction(function (event) { 
     if ($state.current.name == "menu.hPCompliance") { 
     var confirmPopup = $ionicPopup.confirm({ 
      title: 'Exit', 
      template: 'Are you sure you want to close the app?', 
      okText: "Yes", 
      okType: "button-positive", 
      cancelText: "No", 
      cancelType: "button-assertive" 
     }); 
     confirmPopup.then(function (res) { 
      if (res) { 
      navigator.app.exitApp(); 
      } else { 
      console.log('Cancel'); 
      } 
     }); 
     } 
     else { 
     window.history.back(); 
     } 
相關問題