2017-06-14 42 views
0

我已經爲app.run()中的所有狀態編寫$ionicPlatform.registerBackButtonAction。因此,當應用程序初始化時,它爲所有頁面註冊後退按鈕。現在,我想更改特定頁面的硬件後退按鈕功能該頁面的控制器。如何不註冊所有其他頁面。如何禁用Ionic中特定頁面的硬件按鈕

$ionicPlatform.registerBackButtonAction(function() { 
     if ($state.current.name == 'app.homepage' || $state.current.name == 'app.loginUser') { 
      navigator.app.exitApp(); 
     } else if ($state.current.name == "app.contacts" || $state.current.name == 'app.carDocuments' || $state.current.name == 'app.tracking' || $state.current.name == 'app.logBook'|| $state.current.name == 'app.geofence' || $state.current.name == 'app.walktocar' || $state.current.name == 'app.dashboard' || $state.current.name == 'app.geofenceTest' || $state.current.name == 'app.settings' || $state.current.name == 'app.about' || $state.current.name == 'app.tripDetail' ||  $state.current.name == 'app.newTripDetail'|| $state.current.name == 'app.followMeTrack' || $state.current.name == 'app.followMe' || $state.current.name == 'app.maintainance' || $state.current.name == 'app.faultHistory' || $state.current.name == 'app.routeType'|| $state.current.name == 'app.poi') { 
      if ($rootScope.isDeletePage == "true") { 
       $rootScope.$broadcast('callOriginalView'); 
       $rootScope.isDeletePage = "false"; 
      } else { 
       $ionicHistory.nextViewOptions({ 
        disableBack: true 
       }); 

       $state.go('app.homepage'); 
      } 

     } else { 
      $ionicHistory.goBack(); 
     } 
    }, 100); 

因此,如果我再次寫$ ionicPlatform.registerBackButtonAction然後我必須再次寫所有這些代碼註冊。我如何覆蓋特定頁面的後退按鈕只有他的控制器

+0

添加你的示例代碼 –

回答

0

通常,我們不應該重寫默認操作,但如果您想要更改它的功能,請確保使用該功能執行單一功能。

// Disable BACK button on home 
    $ionicPlatform.registerBackButtonAction(function (event) { 
    if($state.current.name=="app.home"){ // here instead of "app.home" mention state name of particular page 
     //navigator.app.exitApp(); // this is for exiting app 
     // write your code that you wish to do 
     event.preventDefault() // do nothing 
    } 
    else if($state.current.name=="app.settings"){ 

     // write your code that you wish to do 

    } 
    else if($state.current.name=="app.contact"){ 

     // write your code that you wish to do 

    } 
    else { 
     navigator.app.backHistory(); 
    } 
    }, 100); 

如果你想在每一頁不同的操作,在服務的功能,並通過將配置類似的回調函數不同的控制器調用它。所以,你不需要在每一個控制器上註冊

+0

,但隨着你的解決方案,它會覆蓋其他頁面註冊回buttton,我也必須再次註冊它們,可以說,如果對於特定狀態爲「app.settings」點擊返回按鈕我必須將其移動到主頁,並且我已經在應用程序加載期間在app.js註冊了此時我不想再次註冊所有按鈕 – user3454799

+0

檢查uopdated答案。 –

+0

我已經在應用程序的初始化過程中編寫了所有這些條件,只有一次我沒有再次註冊它,但對於特定的控制器,我想重寫if條件,並且不想重寫所有這些條件。 – user3454799

相關問題