7

我正在使用以下控制器,以便在dashboard页面中 android 后退按钮退出应用程序,但在其余页面上它会返回。由于在dashboard页面之前我有一个教程,我只向我的用户展示一次,然后我必须覆盖 android 后退按钮,以便在dashboard按下它时退出,它可以很好地使用以下代码:

angular

  .module('az-app')
  .controller('DashboardController', function ($scope, $state, $ionicPlatform) {

    /**
     * While user on dashboard.html we don't want Android back button to return
     * to tutorial views so we override it so that in case that back button is pressed
     * to exit app which is in accordance with android lifecycle.
     *
     */
    $ionicPlatform.registerBackButtonAction(function () {
       if($state.is('/dashboard') || $state.is('dashboard')){
        navigator.app.exitApp();

       }
    }, 100);


  });

现在的问题是,当我转到以下视图时,它仍然可以用作退出按钮,但我希​​望它只是任何其他不是仪表板的视图中的后退按钮,所以我在以下控制器中尝试了这个:

angular

  .module('az-app')
  .controller('DirMedicoController', function ($scope, $state, $ionicPlatform) {

    $ionicPlatform.registerBackButtonAction(function () {

      navigator.app.backHistory();

    }, 100);

  });

所以现在它返回了功能,但是当dashboard我从过去的控制器按下返回时,它会覆盖它的功能,现在而不是退出它会返回。

更新

感谢 mudasser ajaz 在下面的回答,我终于可以让它工作了,答案是:

angular

  .module('az-app')
  .controller('DashboardController', function ($scope, $state, $ionicPlatform, $location, $ionicHistory) {

    /**
     * While user on dashboard.html we don't want Android back button to return
     * to tutorial views so we override it so that in case that back button is pressed
     * to exit app which is in accordance with android lifecycle.
     *
     * Else if not on dashboard just work as normal back button
     *
     */
    $ionicPlatform.registerBackButtonAction(function() {
      if ($location.path() === "/dashboard") {
        navigator.app.exitApp();
      }
      else {
        $ionicHistory.goBack();
      }
    }, 100);


    $scope.backToPolicy = function () {
      $state.go('intro');
    }

    $scope.showDirMedico = function () {
      $state.go('dirmedico');
    }

  });
4

1 回答 1

20

在您的仪表板控制器中执行此操作

$ionicPlatform.registerBackButtonAction(function() {
//var path = $location.path()
  if ($location.path() === "/dashboard" || $location.path() === "dashboard") {
    navigator.app.exitApp();
  }
  else {
    $ionicHistory.goBack();
    //navigator.app.goBack();
  }
}, 100);

并添加 $location 和 $ionicHistory 作为依赖

.controller('DashboardController', function ($scope, $state, $ionicPlatform, $location, $ionicHistory) {

registerBackButtonAction从其他控制器中删除。

于 2015-09-01T21:49:01.807 回答