离子如何以编程方式返回状态

时间:2015-07-31 02:22:31

标签: javascript html angularjs ionic-framework ionic

当我点击ng-click='goBack()'时,我有一个返回按钮。我可以看到浏览器中的网址从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'
                }
            }
        })

})

2 个答案:

答案 0 :(得分:2)

从您想要返回的控制器中调用此方法..

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

答案 1 :(得分:0)

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

$state.transitionTo('main');

这里&#39;主要&#39;是您在urlrouterprovider中定义的视图名称。

OR

您也可以使用$ location

$location.path('main');
不要忘记在控制器的参数中添加$ state或$ location。