ionic android返回鍵

每次點擊返回鍵只會執行一個事件, 在自定義事件中要控制條件不知足時實行原默認動做. 若是隻在一個view中監控, 還須要及時註銷事件.app

http://www.jianshu.com/p/b567cc657a49
http://blog.csdn.net/liangyiyiliang/article/details/53507411ionic

// 返回鍵, view:100, 條件不知足再執行默認事件
$scope.homebackregister = $ionicPlatform.registerBackButtonAction(function(event) {
    function showConfirm() {
        var confirmPopup = $ionicPopup.confirm({
            title: '<strong>退出應用?</strong>',
            template: '你肯定要退出嗎?',
            okText: '退出',
            cancelText: '取消'
        });
        confirmPopup.then(function(res) {
            if (res) {
                ionic.Platform.exitApp();
            } else {
                $rootScope.backcount = 0;
            }
        });
    }

    if ($state.$current.name == 'home.index') {
        $rootScope.backcount = $rootScope.backcount || 0;
        $rootScope.backcount++;
        if ($rootScope.backcount == 2) {
            showConfirm();
        }
        event.preventDefault();
        return false;
    } else {
        navigator.app.backHistory();
    }
}, 101);

//只在當前view下注冊, 退出即註銷事件
$scope.$on('$destroy', $scope.homebackregister);
相關文章
相關標籤/搜索