首页 文章

离子后退按钮的问题

提问于
浏览
1

我的应用程序的后退按钮有问题 .

最初我认为这个问题出现在Cordova,但我发现问题实际上是在Ionic中 .

我在研究解决方案时发现了这段代码:

// Disable BACK button on home
  $ionicPlatform.registerBackButtonAction(function (event) {
    if($state.current.name=="app.home"){
      navigator.app.exitApp();
    }
    else {
      navigator.app.backHistory();
    }
  }, 100);

但是,它给出以下错误:

未捕获的ReferenceError:未定义$ ionicPlatform

我将该代码放在一个名为 functionAngular.js 的新文档中,并将其添加到 body 标记的末尾 . 我必须告知这个功能吗?

我的问题是:

我希望我的后退按钮能够将用户进一步发送回导航堆栈,而不是立即关闭应用程序 .

我很感激这个帮助 .

2 回答

  • 1
    angular.module('EGLISE')
    
    .run(function($ionicPlatform,$state,$ionicHistory){
    
     $ionicPlatform.registerBackButtonAction(function (event) {
        if($state.current.name=="app.home"){
          navigator.app.exitApp();
        }
        else {
           $ionicHistory.backHistory();
        }
      }, 100);
    
    });
    

    请将您的functionAngular.js修改为上面的代码 .

  • 1

    我建议你首先在控制器中添加$ ionicPlatform,并在第一个加载的控制器中,测试每个状态(见下文)后退按钮应该有不同的动作 .

    $ionicPlatform.registerBackButtonAction(function () {
            if ($state.current.name == " login (example) ") {
                ionic.Platform.exitApp();
            }
            if ($state.current.name == " main menu buttons (example) ") {               
                // Sample message "want to exit the application?" (YES/NO)                         
                if (YES) {
                    $ionicViewSwitcher.nextDirection('back');
                    $state.go(' ????');
                };
            };
            if ($state.current.name == " order (example) ") {               
                // Sample message "want to exit the order?" (YES/NO)                         
                if (YES) {
                    $ionicViewSwitcher.nextDirection('back');
                    $state.go(' ????');
                };
            };                        
        }, 100);
    

相关问题