Skip to content

feat($state): expose previous state/params via $state #1218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

homerjam
Copy link

Please please please, fixes #92 and #1100

@timkindberg
Copy link
Contributor

While I think this would make a lot of people happy... I'm reading @nateabele comment from a very similar pull request that was closed.

#861 (comment)

@ericclemmons
Copy link

please please please :) This is the first google result on how to do this :)

@nateabele
Copy link
Contributor

Yeah, this is piling bad design on top of existing bad design. Sorry.

@homerjam
Copy link
Author

I understand, a history service would be more appropriate. This patch will hopefully prove a useful if temporary fix for some in the meantime.

Alternatively this snippet (which could go in the F.A.Q. maybe?) might help:

// in main controller
$rootScope.$on('$stateChangeSuccess', function(e, toState, toParams, fromState, fromParams) {
    $state.previous = fromState;
    $state.previousParams = fromParams;
});

@nateabele
Copy link
Contributor

Fixed that for you: http://plnkr.co/DJH6mQUCbTFfSbdCBYUo

Please note that some features have been left as an exercise for the reader (see TODOs).

@homerjam
Copy link
Author

Thanks @nateabele, it's... beautiful. I'm not sure I quite understand the TODOs but hopefully it'll become clear.

@nateabele
Copy link
Contributor

@homerjam You bet. The purpose of the TODOs has to do with attempting to mirror the history tracking with the browser's actual history as presented in the dropdown menu of the back button. Since not all states have URLs, there won't be a 1:1 correspondence between $history and $window.history.

However, by being clever and tracking which states have URLs, you could do some tricks to match up one to the other, so that you can properly keep the actual browser's history properly in sync. Hope that makes sense.

@homerjam
Copy link
Author

Yes I get what you mean now, I've not really used $window.history but I'll see how it compares and give the matching up a go when I get a moment. Thanks again

@nateabele
Copy link
Contributor

@homerjam Cool, let me know how you get on.

@BorisKozo
Copy link

I cannot find this in version 0.2.11, will it be in the next release?

@nateabele
Copy link
Contributor

@BorisKozo Please note that this PR was closed without being merged.

@amitagrawal11
Copy link

I have created my own service to handle this. Please check it out and let me know if any concern comes

https://github.com/amitagrawal11/UI-Router-History

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Access previous state
6 participants