2

How to config UI Router to reenter or reload the state by default?

E.g. user wants to refresh page, so he clicks the link that follows to that page. But currently the link isn't clickable, as it goes to the same page and the state doesn't change. Refreshing with browser button does work, so it reloads entire SPA again - isn't desirable.

This question - Reloading current state - refresh data - describes similar effect. Is it possible to change this solution - https://stackoverflow.com/a/23198743/404099 - to define the default behavior?

By the way, probably my approach is incorrect and I'm missing some basic idea in UI Router. Please point out if this is the case.

Community
  • 1
  • 1
Ilia Barahovsky
  • 10,158
  • 8
  • 41
  • 52
  • 2
    The concepts of AngularJS are designed to remove the need to refresh a page, since it will be automatically updated due to two-way databinding. Add a refresh button which reloads the data in the controller if you need to update data from the server. Routing is used to define which views to show, not to handle data refreshes. This should be done in the controllers (maybe you can use events to trigger a reload mechanism). – Sabacc Jul 24 '14 at 08:02
  • Yes, I do use AngularJS and two-way data binding works perfectly. The need for refreshes comes from server data. When state changes, it's not only that the views refreshes, but also a newly created instance of controller is assigned to it, and this causes all server-side data to come again. If possible and logical, I would like to spare the need to add Refresh button in every view... – Ilia Barahovsky Jul 24 '14 at 08:54

1 Answers1

4

I second what @Sabacc said, but if you still instead, here you go:

angular.module('app')
.config(function($provide) {
    $provide.decorator('$state', function($delegate)
    {
        $delegate.go = function(to, params, options)
        {
            return $delegate.transitionTo(to, params, angular.extend(
            {
                reload: true,
                inherit: true,
                relative: $delegate.$current
            }, options));
        };
        return $delegate;
    });
});
Kousha
  • 32,871
  • 51
  • 172
  • 296