angular ui router back button issue

こ雲淡風輕ζ 提交于 2019-12-12 03:26:17

问题


'use strict';

angular.module('cbApp')
  .config(function ($stateProvider) {
    $stateProvider
      .state('search', {
        url: '/college/search',
        templateUrl: 'app/collegesearch/views/collegesearch.html',
        controller: 'collegeSearchCtrl'
      })

      .state('searchCollegeFilter', {
        url: '/college/search/:streamId?cities&courses&branches&ordering',
        templateUrl: 'app/collegesearch/views/collegesearch.html',
        controller: 'collegeSearchCtrl'
      });

  });

Here my application calls the 1st state i.e 'search' with a url /college/search. Inside the controller I transition to another state searchCollegeFilter. What I wanna do is navigate the user back to the back they came from when they click the browser back button. Say they came from '/' I want them to go back to home page. But in browser back history there are 2 entries for college/search. I want this to happen only for the 1st time.


回答1:


For this northing is do with angularjs, the thing is you need to watch browser back event before navigating "window.onhashchange". By observing that you can make you check and can redirect default page




回答2:


What I am doing in a different application would serve the purpose:

Basically: Specify parent states!

-> Then the logic is becoming easy.

You don't have to be specific about history or back button or anything like that.

Basically:

-> Check in

  • $rootScope.$on("$onstateChange", ...

-> If

  • the fromState.parent equals toState.parent
  • then $window.history.replaceState({}, "My Detail State Page", $state.url(toState)


来源:https://stackoverflow.com/questions/35599503/angular-ui-router-back-button-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!