$ route.reload()不适用于ui-router

时间:2021-04-03 19:36:54

I've switched to to ui-router. Everything went smoothly, except one thing. On my page I have a select that changes the context of the application. Anyway, previously, when this context was changed I was executing this code (in particular, set method):

我已切换到ui-router。一切顺利,除了一件事。在我的页面上,我有一个选择,可以更改应用程序的上下文。无论如何,以前,当这个上下文被改变时,我正在执行这个代码(特别是set方法):

'use strict';
angular.module('main').factory('lacContext', ['$route', function ($route) {
    return {
        set: function (id) {
            sessionStorage.setItem("lac-context", id);
            $route.reload();
        },
        get: function () {
            return sessionStorage.getItem("lac-context");
        }
    };
}])

and

$route.reload()

was doing the most important thing. It reloaded the page. But after switching to ui-router, $route.reload does nothing. Also I did not find counterpart in ui-router API. How to solve this issue?

做了最重要的事情。它重新加载了页面。但是在切换到ui-router之后,$ route.reload什么也没做。我也没有在ui-router API中找到对应的。如何解决这个问题?

5 个解决方案

#1


36  

How about:

怎么样:

$state.go($state.$current, null, { reload: true });

#2


7  

You can do $state.reload()

你可以做$ state.reload()

There's a bug with it sometimes not re-instantiating the controller. You can get around that with

它有一个错误,有时不会重新实例化控制器。你可以解决这个问题

$state.transitionTo($state.current, $stateParams, { reload: true, inherit: true, notify: true });

#3


5  

Ok it works when I inject $state into controller.

好吧,当我向控制器注入$ state时它会起作用。

But when injecting it into service like code snippet, of course $state was undefined.

但是当它像代码片段一样注入服务时,当然$ state是未定义的。

Although

虽然

$state.go('.')

did not work, I did something like this:

没用,我做了这样的事情:

    $stateProvider
      .state('home', {
          controller: function ($state) {
              $state.go('advisoryLeadOffering.packages');
          }
      })
      .state('advisoryLeadOffering.packages', {
          url: "/packages",
          templateUrl: "/AdvisoryLeadOffering/Packages",
          controller: 'AdvisoryLeadOfferingPackages'
      })

and when I need to reload I do something like this:

当我需要重新加载时,我会做这样的事情:

$state.transitionTo('home');

inside scope's method.

内部范围的方法。

#4


0  

I had a similar problem where I wanted a link outside of the controller to refresh a state and just created a reload() function in the controller.

我有一个类似的问题,我想在控制器外部的链接刷新状态,并在控制器中创建一个reload()函数。

SomeCtrl:

SomeCtrl:

$scope.reload = function(){
    $state.transitionTo('myState');
}

Add this to your anchor:

将其添加到您的锚点:

ng-click="reload()"

H/T @dragonfly for pointing me to transitionTo().

H / T @dragonfly将我指向transitionTo()。

#5


0  

The only thing worked for me:

对我来说唯一有用的东西:

Create redirect state:

创建重定向状态:

$stateProvider.state('redirect', {
    url: 'redirect/:to',
    controller: function($state, $stateParams, $scope) {
        $state.go($stateParams.to, null, {reload: true});
    }
});

Go to this state:

转到这种状态:

$state.go('redirect', {to: $state.current.name}, {reload: true});

#1


36  

How about:

怎么样:

$state.go($state.$current, null, { reload: true });

#2


7  

You can do $state.reload()

你可以做$ state.reload()

There's a bug with it sometimes not re-instantiating the controller. You can get around that with

它有一个错误,有时不会重新实例化控制器。你可以解决这个问题

$state.transitionTo($state.current, $stateParams, { reload: true, inherit: true, notify: true });

#3


5  

Ok it works when I inject $state into controller.

好吧,当我向控制器注入$ state时它会起作用。

But when injecting it into service like code snippet, of course $state was undefined.

但是当它像代码片段一样注入服务时,当然$ state是未定义的。

Although

虽然

$state.go('.')

did not work, I did something like this:

没用,我做了这样的事情:

    $stateProvider
      .state('home', {
          controller: function ($state) {
              $state.go('advisoryLeadOffering.packages');
          }
      })
      .state('advisoryLeadOffering.packages', {
          url: "/packages",
          templateUrl: "/AdvisoryLeadOffering/Packages",
          controller: 'AdvisoryLeadOfferingPackages'
      })

and when I need to reload I do something like this:

当我需要重新加载时,我会做这样的事情:

$state.transitionTo('home');

inside scope's method.

内部范围的方法。

#4


0  

I had a similar problem where I wanted a link outside of the controller to refresh a state and just created a reload() function in the controller.

我有一个类似的问题,我想在控制器外部的链接刷新状态,并在控制器中创建一个reload()函数。

SomeCtrl:

SomeCtrl:

$scope.reload = function(){
    $state.transitionTo('myState');
}

Add this to your anchor:

将其添加到您的锚点:

ng-click="reload()"

H/T @dragonfly for pointing me to transitionTo().

H / T @dragonfly将我指向transitionTo()。

#5


0  

The only thing worked for me:

对我来说唯一有用的东西:

Create redirect state:

创建重定向状态:

$stateProvider.state('redirect', {
    url: 'redirect/:to',
    controller: function($state, $stateParams, $scope) {
        $state.go($stateParams.to, null, {reload: true});
    }
});

Go to this state:

转到这种状态:

$state.go('redirect', {to: $state.current.name}, {reload: true});