-
Notifications
You must be signed in to change notification settings - Fork 3k
One Time Dynamic Param #3047
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
Why do you make the Have you considered updating the data in your component when the parameter changes? http://plnkr.co/edit/0ww2TBT2WNPkZauuvEjT?p=preview |
@christopherthielen Thanks for replying. I'll try to clarify. Here is my route definition: .state('search', {
url: '/search/:key',
params: {
key: ''
},
views: {
'content@': {
component: 'searchComponent'
}
},
resolve: {
navObject: function($stateParams, navStateService) {
return navStateService.getNavState($stateParams.key)
.then(response => {
... returns response
});
},
searchResult: function($stateParams, searchService) {
return searchService.search($stateParams.key)
.then(response => {
... returns response
});
}
}
}) When the resolves finish I have access to both the service.overwriteState = function(overwriteKey) {
$state.go($state.current.name,
{key: overwriteKey},
{location: 'replace'});
}; The goal here is to modify the url such that the overwrite key is now set as the key (for bookmark purposes, among others). However, I do not want the resolve blocks to refire or for the state to be reloaded. Additionally, the previous url with the original key should not be available in the browser history. To solve this, I was attempting to make So the question is: Is there a way to have |
@christopherthielen Any updates on this? Can we make a param dynamic just for a single operation? |
For anyone who still needs this, i've made a workaround/hack for this. It works by temporarily setting the param's dynamic property to true, and restoring it afterwards. http://plnkr.co/edit/hCmot6TK1PMZPW77bvrN?p=preview In this example we want to update the
|
Here there is a somewhat improved version of @MartijnWelker's code, ported to TypeScript: import {StateService} from '@uirouter/core';
export class UiRouterService {
public static readonly $inject = ['$state'];
constructor(private $state: StateService) {}
// Update the URL without page reload!
public updateUrl(params: {[paramName: string]: string}): void {
const paramNames = Object.keys(params);
paramNames.forEach(paramName => this.setDynamicParamState(paramName, true));
this.$state
.go('.', params, {location: 'replace'})
.then(() => paramNames.forEach(paramName => this.setDynamicParamState(paramName, false)));
}
private setDynamicParamState(paramName: string, isDynamic: boolean): void {
let current = this.$state.current.$$state();
while (current) {
const paramDefinition = current.params[paramName];
if (paramDefinition) {
paramDefinition.dynamic = paramDefinition.config.dynamic = isDynamic;
break;
}
if (current.parent) {
current = current.parent;
} else {
throw new Error(`Parameter "${paramName}" is not defined for the current state ("${this.$state.current.$$state().name}").`);
}
}
}
} |
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. |
Hello friends!
I have been working on integrating the 1.0.0-beta.3 version of ui-router into the application I'm working on. In particular, the dynamic param option is the feature I was hoping to take advantage of. Here's my use case:
I have a route defined with this url:
/route/:key
This key has been set to be dynamic.
I am on page one of a list of results and my route url looks like this:
/route/123
When navigating to a second page, I hit an intermediate route:
/nav
Nav's job is to create a new key value. Once returned it navigates to the new page:
route/456
Also, the nav route has
location: 'replace'
set so that it's url no longer shows in the history.The key needs to be set to dynamic because sometimes on loading page one:
start page => nav => page 1
I will get an overwrite key which needs to replace the url's key without reloading the page:
/route/orw123
However, the issue is that now, when I am on page 2 `route/456' when I click the browser back button, it modifies the route url without reloading the page's controllers as only the dynamic key value has changed. This is problematic.
What I see as a possible solution would be to pass a parameter into either a $state.go or a ui-sref options object which would indicate that the key was dynamic for this use only but then act as non-dynamic in all other circumstances.
Is this a viable possibility or is there another alternative I could use for my case?
This is another case associated with #2796 where dynamic params don't seem to be a complete solution to the problem.
The text was updated successfully, but these errors were encountered: