Ionic如何通过编程方式返回上一个状态

5
我有一个返回按钮,当我点击ng-click='goBack()'时,我可以看到浏览器中的url从http://app:8888/#/main/payments变为http://app:8888/#/main/products/7,这是我想要回去的正确路径,但问题是视图没有过渡到那里。
$ionicHistory.goBack();之后,我必须点击刷新按钮才能到达那里或执行window.location.reload
我不想重新加载整个页面,我想过渡到先前的视图。 这是我的html:
        <div class="row">
        <div class="col col-25">
            <button ng-click="goBack()" class="button button-large button-block button-royal">Go Back</button>
        </div>
    </div>

这是我的控制器

.controller('paymentsController', function($scope, $localStorage, $log, $state, $window, $ionicHistory){

$scope.goBack = function(){

    $ionicHistory.goBack();

}


})

这是我的 app.js 文件,我不确定这是否有帮助。

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngStorage'])

.run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if(window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if(window.StatusBar) {
            StatusBar.styleDefault();
        }
    });
})

.config(function($stateProvider, $urlRouterProvider){

    //$ionicConfigProvider.views.transition('none');

    $urlRouterProvider.otherwise('/');

    $stateProvider

        .state('login',{
            url: '/',
            templateUrl: 'templates/login.html',
            controller: 'loginController'
        })

        .state('main', {
            url: '/main',
            templateUrl: 'templates/main.html',
            controller:   'mainController',
            abstract: true
        })

        .state('main.categories', {
            url: '/categories',
            views: {
                'categories': {
                    templateUrl: 'templates/categories.html',
                    controller: 'categoriesController'
                }
            }
        })

        .state('main.products', {
            url: '/products/:productId',
            views: {
                'products': {
                    templateUrl: 'templates/products.html',
                    controller: 'productsController'
                }
            }
        })

        .state('main.payments', {
            url: '/payments',
            views: {
                'payments': {
                    templateUrl: 'templates/payments.html',
                    controller: 'paymentsController'
                }
            }
        })

})

1
可能是重复的问题:如何在页脚中创建返回按钮(Ionic框架)? - callmekatootie
2个回答

2

在您希望返回的控制器中调用此函数。

var backCount = 1;
$rootScope.$ionicGoBack();
$rootScope.$ionicGoBack = function(backCount) {
    $ionicHistory.goBack(backCount);
};

0
使用 $state 重定向到特定视图:
$state.transitionTo('main');

这里的“main”是您在urlrouterprovider中定义的视图名称。

或者

您也可以使用$location。

$location.path('main');

不要忘记将 $state 或 $location 添加到您的控制器参数中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接