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

use*_*830 5 html javascript angularjs ionic-framework ionic

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

我必须点击刷新按钮才能去那里或者做一个window.location.reload之后$ionicHistory.goBack();.

我不想重新加载我希望将该视图转换为前一个视图的整个页面.

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器

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

$scope.goBack = function(){

    $ionicHistory.goBack();

}


})
Run Code Online (Sandbox Code Playgroud)

这是我的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'
                }
            }
        })

})
Run Code Online (Sandbox Code Playgroud)

小智 0

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

$state.transitionTo('main');
Run Code Online (Sandbox Code Playgroud)

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

或者

您还可以使用 $location

$location.path('main');
Run Code Online (Sandbox Code Playgroud)

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