Diego Agher Diego Agher - 1 year ago 116
AngularJS Question

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

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.

Answer Source

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">

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download