0

This question might best be served by showing 2 JSFiddles examples (issue occurs in the 2nd JSFiddle link).

So I was playing with an example in Angular where I go through questions in my app, and when you get to a certain question called 'QS_SEARCH', then it autosubmits the question after 3 seconds. I added some simple functionality so pretend now you are on the last question 'QS_SUCCESS' and if you clicked back to the 'QS_SEARCH' question, then the autosubmit timeout now gets initiated again... but if you click Back one more time quickly before the $timeout func runs, then when function executes it realizes the user isn't on 'QS_SEARCH' anymore and doesn't autosubmit. This is working correctly. http://jsfiddle.net/armyofda12mnkeys/wxLqv4cs/

Now the same example with ui-router seems to hold the old $scope value and will still autosubmit even if you clicked Back to 'QS3' pretend, which is incorrect. How can the $timeout get the updated scope so it doesn't autosubmit when you aren't on that specific question? I even tried $scope.$apply() inside the $timeout function but it still doesn't get the latest value. http://jsfiddle.net/armyofda12mnkeys/qrp6cjgv/

Is this a Closure issue? I thought though $scope would be updated though.

Here is code for the $timeout:

   if($scope.currentquestion.qid == 'QS_SEARCH') {
            console.log('TIMEOUT: FUNC WILL START IN 3s');
            $timeout(function(){
              //$scope.$apply(); //update current question, not working though
              console.log('3S DONE: FUNC EXECUTING');
              console.log('current question is:'+ $scope.currentquestion.qid); 
              if( $scope.currentquestion.qid=='QS_SEARCH' ) {//double-check to make sure stilll on this question!!! (where issue occurs)
                console.log('STILL on QS_SEARCH so autosubmit!');
                $scope.getnextquestion();
              }
            }, 3000);

        }          
armyofda12mnkeys
  • 3,214
  • 2
  • 32
  • 39
  • 1
    You answered yourself in your comments for 2nd fiddle. For each question in this version a new controller is created. Each controller's $scope is isolated. So the $scope for 'QS_SEARCH' continues its life and why should be changed when for a 'back' action a new controller was created with new `id`? – Kirill Slatin May 16 '15 at 08:41
  • Hey @KirillSlatin, Do you think I can keep a variable of the currentquestion.qid in $rootScope then and check that $rootScope.currentquestion.qid in the $timeout? Will try that in a fiddle later today to test it out. EDIT: looks like I can use $rootScope the way I wanted for this: http://jsfiddle.net/armyofda12mnkeys/qrp6cjgv/8/ – armyofda12mnkeys May 16 '15 at 15:13
  • 1
    Using global scope is a bad idea when you're talking about your custom business logic. I would suggest react on destruction of the controller which is performed by ui-router on switching states. Learn more [here](http://stackoverflow.com/a/16096598/4573999). Perhaps you will have to listen to state changing events instead – Kirill Slatin May 16 '15 at 15:19

0 Answers0