8

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):

'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?

7
  • 1
    Have you tried $state.go('.')? Commented Oct 25, 2013 at 7:47
  • 1
    Nope. How can I access current state? Commented Oct 25, 2013 at 7:51
  • 2
    You need to inject the $state service as you do $route then you can call this method. See documentation github.com/angular-ui/ui-router/wiki/… Commented Oct 25, 2013 at 7:54
  • 1
    I did it and $state is undefined Commented Oct 25, 2013 at 8:00
  • 1
    If you have inject the ui-router dependency in module correctly you would get the $state object. See this wiki link with examples github.com/angular-ui/ui-router/wiki Commented Oct 25, 2013 at 8:06

5 Answers 5

37

How about:

$state.go($state.$current, null, { reload: true });
Sign up to request clarification or add additional context in comments.

1 Comment

Shouldn't it be $state.go($state.current, null, { reload: true }); ?
8

You can do $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 });

Comments

6

Ok it works when I inject $state into controller.

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

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.

Comments

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.

SomeCtrl:

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

Add this to your anchor:

ng-click="reload()"

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.