Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic how to go back state programmatically

I have a go back button that when i hit the ng-click='goBack()'. i can see the url in the browser that changes from http://app:8888/#/main/payments to http://app:8888/#/main/products/7 which is the right path that I want to go back to but the problem is that the view doesn't transition there.

I have to hit refresh button in order to go there or do a window.location.reload after the $ionicHistory.goBack();.

I don't want to reload the whole page I want to transition that view to the previous one.

this is my 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>

this is my controller

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

$scope.goBack = function(){

    $ionicHistory.goBack();

}


})

this is my app.js I don't know if this would help.

// 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'
                }
            }
        })

})
like image 348
Millenial2020 Avatar asked Oct 31 '22 22:10

Millenial2020


1 Answers

Call this from your controller where you want to go back..

var backCount = 1;
$rootScope.$ionicGoBack();
$rootScope.$ionicGoBack = function(backCount) {
    $ionicHistory.goBack(backCount);
};
like image 182
roblawford Avatar answered Nov 09 '22 02:11

roblawford