-
Notifications
You must be signed in to change notification settings - Fork 3k
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
How to update params of state without reload of controller? #1758
Comments
reason that it is reloading is because locals of state is changed and doesn't pass this comparison https://github.com/angular-ui/ui-router/blob/master/src/viewDirective.js#L215, I've found one hack to prevent reload of controller it is return previous locale of state: var localeName = 'some-name',
previousLocals = $state.$current.locals[localeName];
$state.go('state', {param: value}, {notify: false});
$timeout(function() {
var newLocals = $state.$current.locals[localeName];
previousLocals.$stateParams = newLocals.$stateParams;
$state.$current.locals[localeName] = previousLocals;
}); but it is just a hack and may won't work after some updates. So it will be better to fix this issue. |
This hack does not work for me, any solutions? Nothing seems to fix this problem! |
0.2.15 has much better implementation of reloadOnSearch... check it out. |
@christopherthielen it's cool, but if you check example http://plnkr.co/edit/i32gBWblr3JaG2aNcnzg?p=preview which uses last 0.2.15 release we still have same problem. I have implement both approach 1st with notify = false, and second with reloadOnSearch = false. 1st ever works better. But still have a bug. |
Agree. I am too using latest version, and have tried both solutions but I'm still experiencing this bug. |
Christopher, do you have any idea for why this situation occurs? |
In @hyzhak plunk, reloadOnSearch is being used wrong.
That said, reloadOnSearch will work properly if:
reloadOnSearch is a bad port of a ngRoute concept, and is superseded by dynamic params in the 1.0 ui-router branch. We won't be allocating any developer time to enhancing reloadOnSearch. If somebody wanted to spend the time to backport dynamic params to 0.2.15, I would consider merging, as long as it is test-compatible with what we have in 1.0. |
Thank you for acknowledging this issue! |
@christopherthielen yes, but I need something other than |
Hi @christopherthielen! I also need the new dynamic params ( |
@Gr33nM0nk Here are a few examples of using reloadOnSearch: @hyzhak @Nandan108 1.0 work in progress is here: https://github.com/nateabele/ui-router/tree/new but is not ready for public consumption. We plan on creating a 1.0 branch in the main ui-router repo within the next few weeks, once we get all the tests passing and 1.0 routing again. |
@christopherthielen thanks for answer, but I'm still thinking that |
We're not going to spend any more effort on state events, since state events are deprecated in 1.0 (if you want them in 1.0+ you'll have to include a compatibility lib). If you have a working hack that works for your use case, I say keep using it. If you have a simple leaf-node state with query params you want to update, use reloadOnSearch=false. |
@christopherthielen my goal is not handle events - my goal is provide 2 way bindings |
I have found the same issue, my controller is run twice after using Switched to using $location for now. But I would much rather use $state's API. |
Hello, Any tips for workaround if parameter is part of path: /search/:term Modified your plnkr for .go bits: Thanks |
@jberzins that's what we're calling "dynamic params", and it's not ready in 0.2.x, sorry. To quote myself:
|
UI-Router 1.0 preview is available: https://github.com/angular-ui/ui-router/tree/feature-1.0 |
@hyzhak Thanks for bringing this "issue" up. I'm encountering a similar situation. I modify the state (selected or not selected) of two radio buttons based on a url parameter When the user modifies the radio button, I need to update the URL. For example, if the user pushes the "Filters" radio button I want the URL to read I can do it with: When I disable updating the URL to match the state of the radio buttons, I can change radio buttons and press the other buttons and although the I tried your workaround, but it was still reloading the entire parent state. Your statement here I think captures the issue perfectly: |
I may have found a way (hack) to make it get the desired effect for my use case, by modifying both the $stateParams and $state.params directly in advance and then using $location.search. So instead of: $state.go('.', {key: value}, notify: false); I used: $stateParams[key] = value;
$state.params[key] = value;
$location.search(key, value); In this way, the my controller won't refresh. I noted that the $stateChange* events don't run, but the the $locationChange* ones do run. @christopherthielen, thanks for the great work on 1.0! Sorry for using such a hacky solution! |
FYI: I have to change the url using: |
original was same as #2087 but then got changed to reloadOnSearch bug along the way |
@eddiemonge scanning this thread and can't figure out what is the "reloadOnSearch bug"? The closest thing I can find is that @jberzins wants dynamic params in the path, not the URL. I feel like this can be closed as dupe |
closing in favor of #2087 |
I was able to solve the issue by using an abstract parent route and a child route described here: http://stackoverflow.com/questions/24581610/ui-router-change-state-without-rerender-reload-of-the-page#answer-24597985
|
@hyzhak, I was probably trying to work-around a similar scenario than yours, and I knew the hack you proposed in this issue was EXTREMELY close to be the final solution for me, but sad enough it was not working (at least with the latest ui-router version My scenario: I have this abstract state After a whole evening of debugging, I realised that faking the local into So, for anyone in my same case: var localeName = 'questions', // <- name of the state that holds the `<ui-view>` in which the pivot state will be embedded (in my case, the parent state for `question` is `questions`).
stateDepth = 1, // <-- Adapt this to your specific case. You need to know how deep your pivot state is in your states hierarchy (starting at 0)
previousLocals = $state.$current.path[stateDepth].locals[localeName];
$state.go(state, {id: id}, {notify: false});
$timeout(function() {
var newLocals = $state.$current.path[stateDepth].locals[localeName];
previousLocals.$stateParams = newLocals.$stateParams;
$state.$current.path[stateDepth].locals[localeName] = previousLocals;
}); Sticking to this until By the way, @christopherthielen, could not find any info/docs about |
Have found this thing to work for me, |
Here the possible solution I found
|
For me |
@albertboada @modikejal10 @kashesandr @muke5hy using You can also try dynamic parameters in the 1.0 version (currently 1.0.0-alpha.3). In your state, configure a parameter as dynamic and implement the uiOnParamsChanged callback :
|
@christopherthielen First of all, thanks for all the work on UI Router! |
@shmool yes, the url should be updating :( I'll take a look |
@shmool have a look at this plunker: http://plnkr.co/edit/MkGSGfPXVC8b6qYqKKjP?p=preview |
@christopherthielen Thanks for the plunker! I thought the feature worked differently - changing the param automatically changes the url. I saw Nate talking about maps with dynamic location coordinates, and I thought it would work this way. But I see I was wrong - I should use actual state transition (with ui-sref or $state.go) to change the url. What's important is that the controller instance is remained. |
@christopherthielen Thanks! I quoted your great responses over at http://stackoverflow.com/a/36600897/682317 |
For those trying to get this to work and can't, can't get the uiOnParamsChanged to fire, or who use Controllers for their components that read the $stateParams vs. setting the Controller in the $stateProvider, I took a different strategy that may help (I'm using Step 1: Setting up your route with the dynamic parameters:$stateProvider
.state('searchresults', {
url: '/search/?:text&:field&:start&:amount&:sortBy&:sortOrder',
params: {
text: {dynamic: true},
field: {dynamic: true},
start: {dynamic: true},
amount: {dynamic: true},
sortBy: {dynamic: true},
sortOrder: {dynamic: true}
},
component: 'ccSearchResults'
}); Step 2. When your Controller first fires, you can use the $stateParams:.controller('searchResultsController', function()
{
...
onSearch(
$stateParams.text,
$stateParams.field,
$stateParams.start,
$stateParams.amount,
$stateParams.sortBy,
$stateParams.sortOrder);
...
}); Step 3. However, when you want to do a new search without reloading your Controller:// vm = this = your controller
$state.go('searchresults', {
text: vm.text,
field: vm.field,
start: vm.start,
amount: vm.amount,
sortBy: vm.sortBy,
sortOrder: vm.sortOrder
})
.then(function(result)
{
onSearch(
$stateParams.text,
$stateParams.field,
$stateParams.start,
$stateParams.amount,
$stateParams.sortBy,
$stateParams.sortOrder);
}); The Promise is important as the $stateParams variable doesn't update until after the Promise has completed. |
@christopherthielen Hello. I am trying to look at your plunk http://plnkr.co/edit/MkGSGfPXVC8b6qYqKKjP?p=preview Is anything missing? |
@ramtob I updated it to angular 1.5.8 and ui-router 1.0.0-beta.3 and it works now: http://plnkr.co/edit/MkGSGfPXVC8b6qYqKKjP?p=preview |
@christopherthielen For some reason I do not see your changes on this link. But after I made these changes myself, the plunk does work. Thanks! |
@christopherthielen the plunkr is using angular 1.4.9, maybe a change wasn't saved? as @ramtob said it works if updated to 1.5.8 in the script tag in index.html |
@marcstober the plunk was "frozen" to an old version. I've unfroze it. Thanks! |
I spent hours with UI-Router 0.3.2 to disable reloading of parent controller when parameter change. Thanks guys for introducing dynamic parameters in 1.0.0 version, it resolve my problem perfectly. |
http://plnkr.co/edit/i32gBWblr3JaG2aNcnzg?p=preview
after you have something like:
any other command like:
or ever
reload controller.
Steps to reproduce:
ReloadOnSearch
Doesn't work because doesn't update params of state and if we go to 'state.index1' we will lost all params from of 'state'.
The text was updated successfully, but these errors were encountered: