Angular - UI Router - state reentrance

2019-07-19 19:51发布

问题:

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.

回答1:

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;
    });
});