-
Notifications
You must be signed in to change notification settings - Fork 3k
1.0 URL change without changing state #2679
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
Comments
@orangesoup did you try using dynamic parameters? I think it may be a good solution for you.
Then transition to the same state, changing only the modalName param: Tell me if it works out for you :) |
I'm not quite sure it works for me (but I might be missing something). Let's say I have a state:
And another state:
When I'm on the profile page I click on a button and then the modal opens. At this point I want to do something like The point is, I want the url to be what is defined for the state |
@orangesoup My Idea is that the param is the indication of whether and which modal is open. So you'll have one state with Routing to Another method you should consider is having the modal states as children of the base view. This way the base will not be exited when you open the modal or re-entered when you close it, so its resolved data will be remained. And you'll be able to attach different controllers and resolves to the modal via their states. But then you can't have a different URL path for the two situations: either have If anyone has another idea, I'll be happy to hear. (I'm investigating UI-Router thoroughly these days preparing for a lecture about routing in Angular...) |
@shmool Thanks for the suggestions! The first one is good'ish, but still it's not quite what I want. Still, the idea is good, thanks for the explanation! However, I'm wondering whether it is such an edge use case really. There is a very similiar use case on Instagram for example. When someone clicks on an image, the url changes, after closing the modal the url goes back to the original one. When you reload the page when the modal is open, then the actual state gets loaded fully, not the modal. Basically this is what I would like to get. |
For now I decided to use I still think it's a hacky way to do the job, but until there is a better solution I'm gonna stick to this I guess. |
Wow, this a great discussion! @orangesoup your use case can be solved using "sticky states" from ui-router-extras.
A transition from Refreshing the browser on the modal would enter Unfortunately, sticky states is not yet compatible with the 1.0 alpha. I have plans to make it compatible, but I am currently devoting time to finalizing 1.0, and haven't spent much on extras. |
@christopherthielen Heh, thanks! I just upgraded to 1.0 ~2 days ago to see if I can solve this problem with it. Unlucky, lol. The hacky way I mentioned above is kinda buggy when using the back button and such, so I might just revert back to 0.2.x and use ui-router-extras then! Is there any ETA for the 1.0 compatibility? |
@orangesoup there is no ETA currently. I will probably have to wait until after 1.0 is in release candidate, and after ui-router-ng2 (angular 2 version) is in beta. |
Hello do you have find a solution ? I can't trust the users input (Who can write space between words, apostrophes, uppercases, etc). I want redefined it like a great url SEO friendly when the resolve function returns me success. Why ? because my webservice send me the trust slug that I can use. Example : The user writes : I want reverse the user inputs with this slug like this before the templates load : I'm using UI router, it is a way for do this ($state, $stateparams) ? Here is the part of my router code which work with : .state('villes.ville', {
url: "/:ville",
parent: 'villes',
templateUrl: "partials/ville.tpl.html",
metaTags: {
title: '{{city.label}} ({{city.zip_code}})',
},
resolve: {
city: function(City, $state, $stateParams){
return City.get({
slug: $stateParams.ville,
shape: true,
details: true
}).$promise.then(
function(data){ return data },
function(error){ $state.go('/404') });
}
},
controller : 'villeCtrl'
}) I tried to inject |
@ShakurOo are you using ionic? Noticed |
Oups I forgot to take off it, no I don't use ionic @christopherthielen . |
@ShakurOo I think you can use ui-router 1.0 hook to validate the parameter before allowing the transition. $transitions.onBefore({ to: 'villes.ville' }, function($transition$, $injector) {
let slugService = $injector.get('SlugService');
let ville = $transition$.params().ville;
// user provided arbitrary input and we don't recognize it as a the canonical param
if (!slugService.isCanonical(ville)) {
// return a promise from the hook. the promise fetches the canonical slug
return slugService.loadCanonicalSlug(ville).then(function (canonicalSlug) {
// after the slug is loaded, redirect to the same state with the canonical param value
return $injector.get('$state').target($transition$.to(), { ville: canonicalSlug }, { location: 'replace' });
});
}
}); Then you just need to implement |
How thank's @christopherthielen I never used this before. I using the latest version of UI Router (V0.2.18) but I can migrate to the V1. I'll be back when I have succeeded integrating this hook :) |
@christopherthielen I tried to implement your hook in the run() but I have a infinite loop : I ask myself if the return $transition don't reload the entiere state ? angular.module('app.config', [])
.run(function($rootScope, $state, $stateParams, $transitions){
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
$transitions.onBefore({ to: 'villes.ville' }, function($transition$, $injector) {
var ville = $transition$.params().ville;
var slugService = $injector.get('Cities');
return slugService.getSlug(ville).then(function (canonicalSlug) {
// The console.log loop
console.log(canonicalSlug)
return $injector.get('$state').target($transition$.to(), { ville: canonicalSlug }, { location: 'replace' });
});
});
});` |
Hello! A year has passed almost since the last comment, and it seems I have stumbled onto this issue as well. My use case is as follows. I am displaying a bookings page, which has a date picker on it. The currently active date is always present in the URL as a state parameter, so that people can copy paste the URL easily or bookmark certain dates etc. The problem is that when a date changes, the controller's I used to tackle this with the I tried to use Any thoughts on what I can do to prevent the
So assuming when This is quite crucial to our application, and I would prefer not to resort to having to update the URL manually using the Edit: actually event just changing the path with the Looks like ui-routers states are really persistently linked to the URL. Is changing it without changing state not possible at all? |
Upon yet further digging and deep analysis of the docs, I've stumbled upon the https://ui-router.github.io/ng1/docs/latest/interfaces/params.paramdeclaration.html#dynamic I think that probably closes this issue as well! It might be good to mention the existence of this parameter in the deprecated |
@adamreisnz thank you very much for your solution and saved my time! I spent several hours to fix this problem and read the documentation. But I would have spent more time if not for you. |
Glad I could help! |
Hi, I can't get it to work. Given this state declaration
when trying to update the id and searchType params the URL doesn't change.
I've checked the output of the |
@sztrzask according to this doc: https://github.com/angular-ui/ui-router/wiki/Quick-Reference#stategoto--toparams--options |
This issue has been automatically marked as stale because it has not had This does not mean that the issue is invalid. Valid issues Thank you for your contributions. |
I have a state, let's call it
x
. I can open many modals (one at the same time) on this page/state. What I would like to do is when I open a modal I change the "state" toy
(with$state.go
). I say "state", because I only want to change the url, nothing else. In the old version I could do a simple$state.go(..., ..., {location: 'replace', notify: false})
(I know it wasn't intended for this, and it wasn't even perfect since it always re-resolved the stuff...).Also when I close the modal I want to change the url back to the original one. I don't want to re-render, re-resolve anything. Just simply change the url.
I know there's plenty of new hooks in 1.0 (I'm using alpha.4), but I simply can't find a way to do this.
Any help would be appreciated!
The text was updated successfully, but these errors were encountered: