我正在使用以下控制器,以便在仪表板页面安卓后退按钮退出应用程序,但在其余页面上它返回.因为在仪表板页面之前我有一个教程我只给我的用户一次,然后我不得不重写
android后退按钮,以便在仪表板中退出,如果按下它,它适用于此代码罚款:
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',$ionicPlatform) { $ionicPlatform.registerBackButtonAction(function () { navigator.app.backHistory(); },100); });
所以现在它提供了功能,但是再次在仪表板上如果我按回来自过去的控制器它会覆盖它的功能,现在而不是退出它返回.
UPDATE
感谢下面的回答mudasser ajaz我终于可以做到它的工作答案是:
angular .module('az-app') .controller('DashboardController',$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'); } });
解决方法
在仪表板控制器中执行此操作
$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',$ionicHistory) {
从其他控制器中删除registerBackButtonAction.