4

In this plunk I have a ui-router $state.goto that only works the first time, even though I'm incrementing a seq variable to force the reload. See that the time stamp doesn't change. What's wrong with this code?

HTML

<body ng-controller="MyCtrl">
    <button ng-click="goto()">Go to state</button>
    <div ui-view=""></div>
</body>

State HTML

  <div>
    {{time1}}
  </div>

Javascript

var app = angular.module("app", ['ui.router']);

app.controller('MyCtrl', function($scope,$state) {

var seq = 0;

$scope.goto = function(){

    alert('about to go to state - ' + seq) ;

        var params = { 
                seq: seq++ 
               };

        $state.go ( 'state1', params );

  };
});

app.controller('state1Ctrl', function($scope) {

      $scope.time1 = new Date();

});


app.config(function($stateProvider) {

  $stateProvider
    .state('state1', {
         templateUrl: 'state1.html',
         controller: 'state1Ctrl'
    });

});
2
  • You are passing a parameter to your state but in your url u don't specify one. Commented Mar 2, 2015 at 16:03
  • I don't have a url, just the state name Commented Mar 2, 2015 at 16:10

2 Answers 2

4

Since you're not actually changing state (i.e., you're hitting state1 every time, just with a different parameter), you have to pass the third options parameter with {reload: true}

$state.go('state1', params, {reload: true});

Your plnkr, updated

See the documentation for $state.go(to, params, options)

Edit (to make this answer correct based on the comments we have below)

You aren't including the params in the actual state, so it's not listening for them, hence when you send them it doesn't automatically trigger a reload. Your state should be:

$stateProvider
  .state('state1', {
    templateUrl: 'state1.html',
    controller: 'state1ctrl',
    params: {
      seq: {}
    }
  });

Here, the $stateProvider automatically sets reloadOnSearch = true, you could do the opposite of what you're asking by using a third parameter in your state, reloadOnSearch: false

Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying to avoid reload:true
So, your current state doesn't actually listen for any parameters (but you're passing it parameters), if it did listen for parameters, it would automatically reloadOnSearch
i was also missing {reload: true} it saves my live thank you
0

The params keyword is missing, needs to be declared in $stateProvider when you use parameters:

 $stateProvider
    .state('state1', {
         templateUrl: 'state1.html',
         params: { 'seq': null },
         controller: 'state1Ctrl'
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.