【问题标题】:Ionic how to go back state programmatically离子如何以编程方式返回状态
【发布时间】:2016-10-11 20:45:47
【问题描述】:

当我点击ng-click='goBack()' 时,我有一个返回按钮。我可以在浏览器中看到从 http://app:8888/#/main/payments 更改为 http://app:8888/#/main/products/7 的 url,这是我想返回的正确路径,但问题是视图不会在那里转换。

我必须点击刷新按钮才能到那里或在$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'
                }
            }
        })

})

【问题讨论】:

标签: javascript html angularjs ionic-framework ionic


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    $state.transitionTo('main');
    

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

    您也可以使用 $location

    $location.path('main');
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      相关资源
      最近更新 更多