Pavan
Pavan

Reputation: 1015

angular ui router back button issue

'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.

Upvotes: 0

Views: 1011

Answers (2)

Joehannes
Joehannes

Reputation: 301

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)

Upvotes: 1

Kamal Raj
Kamal Raj

Reputation: 82

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

Upvotes: 1

Related Questions