reloadOnSearch: false will not change the location url
I'm trying to use the reloadOnSearch config property (v0.2.7) to disable reloading the state to navigate between multiple pages of a product list. My state is configured as follows:
$stateProvider.
state('app.account.products', {
url: '/products/preview?page&sort',
templateUrl: 'products.preview.html',
reloadOnSearch: false,
controller: 'ProductsCtrl',
data: { title: 'Preview' }
}).
Pager links (on ngclick):
$stateParams.page = pageNum;
$state.go($state.current.name, $stateParams, { reloadOnSearch: false });
The paging works fine without any reloads, however, it does not refresh the url, i.e. if I'm on app/products/preview and I move to the next page app/products/preview?page=2&sort=asc, the url in the address bar still shows app/products/preview for all subsequent pages. Is this by design? If so, how can I resync the $location based on the search string?
reloadOnSearch is really meant to be used with $location.search(), or at least that's the specific use case that caused it to be added. I'm not saying you shouldn't be able to do it this way, but would you be okay with just doing this instead:
$location.search({page:pageNum, sort:"asc"});
The reason I'm using $state.go is because the paging links are externally routable, i.e. you can copy paste the link with the query string containing the page number and sort order and be able to get to the right page. With $location.search only the url changes and any watches listening to the $stateParams do not fire and neither does the $state route to the right page.
The current setup that I have (with $state.go and reloadOnSearch) works well, except that when I'm navigating internally, i.e. clicking the paging links, does not change the search string.
Would need a plunkr to look into this more.
Ok, finally got around to setting this up:
http://plnkr.co/edit/F3zt7b5XwzxappsclIgm?p=preview
As you can see, the pagination links prevent the default href navigation and instead call $state.go with reloadOnSearch=true. In this way if the user were to right click and open a new tab/window (or copy paste the url in the address bar), then the url with the paging query string would reroute them to the right page number. Also note that the controller is reloaded for every $state.go despite there being a reloadOnSearch set to true.
I'm not exactly sure what to tell you. ReloadInSearch was written to work only with $location,.search() so I'm having trouble understanding how this is a bug.
Same feeling here : this is an unexpected behaviour.
When you use $location.search to update the url without reload the page, changes are not reverberate by $state and $stateParams.
Look at this example : http://plnkr.co/edit/29oe2EVTzUhS0IUDqZIk Live : http://run.plnkr.co/IqbFXP0aoINwBY86/#/news?page=1
In this example, use of $state.href become deprecated.
When using reloadOnSearch=false, $state.transitionTo should update the url without reload the page, as the use of $location.search.
I am also having issues. It seems that when work with reloadOnSearch, ui-sref doesn't take searchParams changes or stateParam changes into account. reloadOnSearch defiantly needs more attention.
@mmukarram Your Plunkr works exactly as expected. Also, this:
$state.go($state.current.name, $stateParams, { reloadOnSearch: false });
...is invalid. reloadOnSearch is not a valid option in go().
@bdecarne Your Plunkr doesn't load.
Ultimately, reloadOnSearch is a kludge that was added to solve a very narrow use case, and is being removed when dynamic parameters are fully implemented.
I also just ran into a problem with reloadOnSearch. Here is a plunker: http://plnkr.co/edit/2lgzlKe7E8m56AHLDnou
When you click on any of the links, nothing happens. I don't think this is expected behaviour. The $stateChangeSuccess event should be triggered.
@Elijen +1
Have found this thing to work for me, $state.go($state.current, {params},{notify:false,reload:$state.current});
Thanks @modikejal10, that worked for me too!
Thanks @modikejal10 it works :)
Warning, {notify: false} is deprecated