有角度的phonegap后退按钮太深了

时间:2017-07-31 23:08:31

标签: angularjs back-button phonegap

我用以下角度js路线写了一个phonegap应用程序

app.config(function($routeProvider) {
    $routeProvider
    .when("/", {
        templateUrl:"views/index.html" /*global menu*/
    })
    .when("/home", {
        templateUrl:"views/home.html" /*item categories*/
    })
    .when('/listing',{
        templateUrl: 'views/listing.html' /*items in selected category*/
    })
    .when('/item',{
        templateUrl: 'views/item.html' /*single item page*/
    })
    .when('/about',{
        templateUrl: 'views/about.html' /*about us*/
    })
    .when('/settings',{
        templateUrl: 'views/settings.html' /*user settings/defaults */
    })
    .otherwise({
        redirectTo: '/'
    });
});

当我来回浏览不同的视图并点击后退按钮时,它在我访问的所有视图中的顺序相反,看起来很尴尬。
相反它应该回到索引(/),如果我点击后退按钮在索引(/),应用程序应退出而不是通过整个历史。
我尝试了以下代码但没有获得正确的功能

var h=[];  //history array
$scope.$on('$locationChangeStart', function(event, newUrl, oldUrl){ 
    if(h.length>0 && h[h.length -2] == newUrl){
        event.preventDefault();
        var currPath = oldUrl.substring(oldUrl.indexOf('#')+1);
        if(currPath=='/item'){
            $location.path('/listing');
        }else if(currPath=='/listing'){
            $location.path('/home');
        }else if(currPath=='/home' || currPath=='/about' || currPath=='/settings'){
            $location.path('/');
        }else if(currPath=='/'){
            navigator.app.exitApp();
        }
        h.pop();
    }else{
        h.push(newUrl); 
    }   
});   

它没有正确回归,有些时候甚至在录制某些链接时表现得像回来,有些时候在后面没有执行预期的操作。
用上面的功能或者如果有其他替代方法来纠正我

后退按钮应该用作

note current_view =返回之前的当前路线

if current_view == '/item'
    转到/listing路线

elseif current_view == '/listing'
    转到/home路线

elseif current_view == '/home' || current_view == '/about' || current_view == '/settings'
    转到/路线

elseif current_view == '/'
    退出申请

0 个答案:

没有答案