Ionic如何以编程方式返回状态

Ionic how to go back state programmatically

本文关键字:方式 返回 状态 编程 Ionic      更新时间:2023-09-26

我有一个返回按钮,当我点击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'
                }
            }
        })
})

从你想要返回的控制器调用它。

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

使用$state重定向到特定视图:

$state.transitionTo('main');

这里的'main'是你在urlrouterprovider中定义的视图名。

也可以使用$location

$location.path('main');

别忘了在你的控制器参数中添加$state或$location