2017-06-14 44 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

相关问题