Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the previous url using $ionicHistory.viewhistory?

I want to track the page where the user was previously than the current one. I was trying to use $ionicHistory.viewHistory() but I can manage to get it. I'm new to angularjs and $ionic so I guess this is a newbie question.

This whole thing is with the purpose of doing a funnel analysis for a single page web app using Piwik but since it doesn't have a proper funnel plugin (thanks Piwik) I'll have to implement it myself. So if you have any advice on the topic it would be great to hear from you.

like image 463
Diego Aguado Avatar asked Dec 09 '22 02:12

Diego Aguado


2 Answers

So what you are looking for is $ionicHistory.backView() or $ionicHistory.backTitle(). If you are using angular states and routing and views.

So in a controller you would call:

$scope.lastView = $ionicHistory.backView() 

This gives the the last view you were in. So if you go from A to B it will give you A.

If you want the title of the last view call this in a controller:

$scope.lastViewTitle = $ionicHistory.backTitle()

This gives you a string, which is whatever title you gave the previous view. This would return "My Page" if you called backTitle() after you just left this view.

<ion-view view-title="My Page">
    <ion-content>
      Hello!
    </ion-content>
  </ion-view>

You can read more here: http://ionicframework.com/docs/api/service/$ionicHistory/

If you dont want views but states, you should be able to grab the previous state on $stateChangeSuccess that is broadcast.

$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
   //assign the "from" parameter to something
});
like image 142
Jess Patton Avatar answered Dec 11 '22 07:12

Jess Patton


var history = $ionicHistory.backView();//returns an object with data about the previous screen. console.log(history) to see the entire data.

var previous_statename = history.stateName;//compare this
var previous_url = history.url;
var previous_title = history.title;
like image 43
HIRA THAKUR Avatar answered Dec 11 '22 07:12

HIRA THAKUR