2015-01-19 9 views
7

Si verificano problemi nell'utilizzo di $ionicHistory nelle pagine in cui vengono utilizzati i valori ion-tabs. Lo uso per navigare alla vista precedente (usando goBack()). Quando inserisco le schede in una vista, la cronologia è errata, la vista posteriore è due viste precedenti.

Per dimostrarlo, ho creato un'applicazione demo (plunker here) con 4 pagine/viste. Pagina 1 -> Pagina 2 -> Pagina 3 -> Pagina 4. L'ultima pagina contiene schede.

angular 
.module("demoapp", ['ionic']) 
.config(['$stateProvider', '$urlRouterProvider', 
    function($stateProvider, $urlRouterProvider){ 
    $stateProvider 
     .state('first', { 
      url: '/', 
      controller: 'firstController', 
      templateUrl: 'first.html', 
     }) 
     .state('second', { 
      url: '/second', 
      controller: 'secondController', 
      templateUrl: 'second.html', 
     }) 
     .state('third', { 
      url: '/third', 
      controller: 'thirdController', 
      templateUrl: 'third.html', 
     }) 
     .state('fourth', { 
      url: '/fourth', 
      controller: 'fourthController', 
      templateUrl: 'fourth.html', 
     }); 

    $urlRouterProvider.otherwise("/"); 
    }]) 
.factory("historyFactory", ['$ionicHistory', function($ionicHistory){ 
    var show = function() { 
    var text = ""; 

    var vh = $ionicHistory.viewHistory(); 
    if(vh !== null) { 
     text += "VIEWS=" + JSON.stringify(vh.views); 
     text += "BACK=" + JSON.stringify(vh.backView); 
    } 

    return text; 
    } 

    return { 
    show : show, 
    } 
}]) 
.controller("firstController", [ 
    '$scope', 
    '$location', 
    function($scope, $location){ 
    $scope.next = function() { 
    $location.path("/second"); 
    }; 
}]) 
.controller("secondController", [ 
    '$scope', 
    '$location', 
    '$ionicHistory', 
    'historyFactory', 
    function($scope, $location, $ionicHistory, historyFactory){ 
    $scope.next = function() { 
     $location.path("/third"); 
    }; 

    $scope.prev = function() { 
     $ionicHistory.goBack(); 
    }; 

    var init = function() { 
     $scope.data = historyFactory.show(); 
    }; 

    init(); 
}]) 
.controller("thirdController", [ 
    '$scope', 
    '$location', 
    '$ionicHistory', 
    'historyFactory', 
    function($scope, $location, $ionicHistory, historyFactory){ 
    $scope.next = function() { 
     $location.path("/fourth"); 
    }; 

    $scope.prev = function() { 
     $ionicHistory.goBack(); 
    }; 

    var init = function() { 
     $scope.data = historyFactory.show(); 
    }; 

    init(); 
}]) 
.controller("fourthController", [ 
    '$scope', 
    '$ionicHistory', 
    'historyFactory', 
    function($scope, $ionicHistory, historyFactory){ 
    $scope.prev = function() { 
     $ionicHistory.goBack(); 
    }; 

    var init = function() { 
     $scope.data = historyFactory.show(); 
    }; 

    init(); 
}]); 

Questo è come la vista con schede assomiglia:

<ion-view> 

    <ion-tabs class="tabs-balanced"> 
    <ion-tab title="Tab One"> 
     <ion-header-bar class="bar-balanced"> 
     <div class="buttons"> 
      <button class="button button-icon ion-ios-arrow-back" ng-click="prev()"></button> 
     </div> 
     <h1 class="title">Page 4 - Tab 1</h1> 
     </ion-header-bar> 

     <ion-content class="has-header"> 
      <h3>History</h3> 
      <p>{{data}}</p> 
     </ion-content> 
    </ion-tab>  
    </ion-tabs> 

</ion-view> 

Nella seconda pagina, la storia vista è simile al seguente:

Sulla terza pagina, un altro guarda l'anteprima:

VIEWS= 
{"002":{"viewId":"002","index":0,"historyId":"root","backViewId":null,"forwardViewId":"003","stateId":"first","stateName":"first","url":"/"}, 
"003":{"viewId":"003","index":1,"historyId":"root","backViewId":"002","forwardViewId":"004","stateId":"second","stateName":"second","url":"/second"}, 
"004":{"viewId":"004","index":2,"historyId":"root","backViewId":"003","forwardViewId":null,"stateId":"third","stateName":"third","url":"/third"}} 
BACK= 
{"viewId":"003","index":1,"historyId":"root","backViewId":"002","forwardViewId":"004","stateId":"second","stateName":"second","url":"/second"} 

Ma nella quarta pagina, con lo ion-tabs la cronologia delle viste rimane la stessa.

VIEWS= 
{"002":{"viewId":"002","index":0,"historyId":"root","backViewId":null,"forwardViewId":"003","stateId":"first","stateName":"first","url":"/"}, 
"003":{"viewId":"003","index":1,"historyId":"root","backViewId":"002","forwardViewId":"004","stateId":"second","stateName":"second","url":"/second"}, 
"004":{"viewId":"004","index":2,"historyId":"root","backViewId":"003","forwardViewId":null,"stateId":"third","stateName":"third","url":"/third"}} 
BACK= 
{"viewId":"003","index":1,"historyId":"root","backViewId":"002","forwardViewId":"004","stateId":"second","stateName":"second","url":"/second"} 

Si tratta di un bug con $ionicHistory quando si utilizza ion-tabs o sto facendo qualcosa di sbagliato nella visualizzazione schede?

+0

Ho più o meno lo stesso problema qui: http://stackoverflow.com/questions/28004332/ionic-tabs-and-side-menu-history. Hai provato a posizionare il quarto stato all'interno del terzo? '.state ('third.fourth') {...' e per navigare lì '/ terzo/quarto' – Miquel

risposta

1

Cercate di avvolgere di ioni schede per evitare questo problema

<ion-view> 

    <div> 
    <ion-tabs class="tabs-balanced"> 
    <ion-tab title="Tab One"> 
     <ion-header-bar class="bar-balanced"> 
     <div class="buttons"> 
      <button class="button button-icon ion-ios-arrow-back" ng-click="prev()"></button> 
     </div> 
     <h1 class="title">Page 4 - Tab 1</h1> 
     </ion-header-bar> 

     <ion-content class="has-header"> 
      <h3>History</h3> 
      <p>{{data}}</p> 
     </ion-content> 
    </ion-tab> 

    <ion-tab title="Tab Two"> 
     <ion-header-bar class="bar-balanced"> 
     <div class="buttons"> 
      <button class="button button-icon ion-ios-arrow-back" ng-click="prev()"></button> 
     </div> 
     <h1 class="title">Page 4 - Tab 2</h1> 
     </ion-header-bar> 

     <ion-content class="has-header"> 
      <p>Content of tab 2.</p> 
     </ion-content> 
    </ion-tab> 

    </ion-tabs> 
    </div> 

</ion-view> 

http://plnkr.co/edit/pVDu9e7QZHzMt3A154i7?p=preview

0

Il ion-tabs introdurre pile parallele per mantenere la storia di ogni scheda e così il $ionicHistory.goBack cambia lo stato storia nel stack corrente. Puoi risolvere il problema rimuovendo la scheda o aggiungendo le schede a tutte le visualizzazioni. C'è una spiegazione eccellente per questo in questo SO messaggio: https://stackoverflow.com/a/31385026/3878940

0

Che cosa ha funzionato per me è stato l'aggiunta di uno ione-view vuoto sopra ioni schede nella parte superiore della pagina.

Immagino che questo abbia "ingannato" la $ ionicHistory facendogli credere che la mia pagina stava rendendo una vista normale piuttosto che una vista a schede.

Problemi correlati